gpt4 book ai didi

rust - 找不到 nickel.rs 帖子重定向

转载 作者:行者123 更新时间:2023-11-29 08:35:26 26 4
gpt4 key购买 nike

我正在使用 nickel.rs、PostgreSQL 和 Angular.js。我可以使用 HTTP POST 插入到我的表中:

// insert
{
let conn = shared_connection.clone();
router.post("/api/movies", middleware! { |request, mut response|
let conn = conn.lock().unwrap();
let stmt = match conn.prepare("insert into movie (title, releaseYear, director, genre)
values ($1, $2, $3, $4)") {
Ok(stmt) => stmt,
Err(e) => {
return response.send(format!("Preparing query failed: {}", e));
}
};

let movie = request.json_as::<MovieInsert>().unwrap();
match stmt.execute(&[
&movie.title.to_string(),
&movie.releaseYear,
&movie.director.to_string(),
&movie.genre.to_string()
]) {
Ok(v) => println!("Inserting movie was Success."),
Err(e) => println!("Inserting movie failed. => {:?}", e),
};

// ERROR (1)
// return response.set(Location("/".into()));

});
}

我知道这很好用,因为该行已插入到 PostgreSQL 表中。但是,Chrome 网络浏览器显示错误:

POST http://localhost:6767/api/movies 404 (Not Found)

我还在ERROR (1)中添加了代码行

response.set(Location("/".into()));

然而,控制台显示错误。

 expected `core::result::Result<nickel::middleware::Action<nickel::response::Response<'mw, _>, nickel::response::Response<'mw, _, hyper::net::Streaming>>, nickel::nickel_error::NickelError<'mw, _>>`,
found `&mut nickel::response::Response<'_, _>`
(expected enum `core::result::Result`,
found &-ptr)

现在我的代码应用了 Shepmaster 所说的内容。

// insert
{
let conn = shared_connection.clone();
router.post("/api/movies", middleware! { |request, mut response|
let conn = conn.lock().unwrap();
let stmt = match conn.prepare("insert into movie (title, releaseYear, director, genre)
values ($1, $2, $3, $4)") {
Ok(stmt) => stmt,
Err(e) => {
return response.send(format!("Preparing query failed: {}", e));
}
};

let movie = request.json_as::<MovieInsert>().unwrap();
match stmt.execute(&[
&movie.title.to_string(),
&movie.releaseYear,
&movie.director.to_string(),
&movie.genre.to_string()
]) {
Ok(v) => println!("Inserting movie was Success."),
Err(e) => println!("Inserting movie failed. => {:?}", e),
};

response.set(StatusCode::PermanentRedirect)
.set(Location("/".into()));
""
});
}

但是错误发生了。

src/main.rs:155:18: 155:43 错误:特征 modifier::Modifier<nickel::response::Response<'_, _>>未针对 hyper::header::common::location::Location 类型实现[E0277]src/main.rs:155 .set(位置("/".into()));

终于这样修好了!

            Ok(v) => {
println!("Inserting movie was Success.");
response.set(StatusCode::Ok);
},
Err(e) => println!("Inserting movie failed. => {:?}", e),
};

//response.set(StatusCode::PermanentRedirect)
// .set(Location("/".into()));
//""
return response.send("");

最佳答案

当前列出的代码没有任何意义。您的路由处理程序没有返回任何内容:

match ... {
Ok(v) => println!("Inserting movie was Success."),
Err(e) => println!("Inserting movie failed. => {:?}", e),
};

因为数据库和前端框架都不重要,所以你的代码等同于:

#[macro_use]
extern crate nickel;

use nickel::{Nickel, HttpRouter};

fn main() {
let mut server = Nickel::new();

server.post("/api/movies", middleware! { |request, mut response|
println!("Hello");
});

server.listen("127.0.0.1:6767");
}

如果您从处理程序返回某些内容,则 HTTP 状态代码会从 404 更改为 200。

如果您想重定向到某个地方,您需要明确更改状态代码。 Response::set 的文档刚好有个例子:

server.get("/a", middleware! { |_, mut res|
res.set(StatusCode::PermanentRedirect)
.set(Location("http://nickel.rs".into()));

""
});

请注意,返回的是一个空字符串,与之前类似。您试图直接返回 Response 类型。

关于rust - 找不到 nickel.rs 帖子重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35323257/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com