'get' 从本地托管服务器获取请求时出现 404 错误
404 Error with 'get' fetch request from locally hosted server
我正在使用我学到的原则构建一个简单的博客 here(使用 Express 和 MongoDB 的 CRUD 应用程序)。为了编辑以前发布的 post,我想用博客 post 的内容填充一个文本字段,该博客的标题是从 drop-down 菜单中选择的。这样我就可以轻松地进行更改并提交它们。
我写了一个基本的 'put' 获取请求,它除了在出现问题时报告错误外什么都不做:
fetch('articles', {method: 'put'})
.then(
function(response) {
if (response.status !== 200) {
console.log('Looks like there was a problem. Status Code: ' + response.status);
return;
}
// Examine the text in the response
response.json().then(function(data) {
console.log(data);
});
}
)
.catch(function(err) {
console.log('Fetch Error :-S', err);
});
这里没有出错,所以console.log(data)
被执行了:
但是,当我将 'put' 更改为 'get'(并且不进行其他更改)时,出现 404 错误:
为什么'get' fetch 请求会导致404 错误,而'put' 请求不会?我在本地托管此应用程序,所以我 运行 来自 localhost:3000。我的服务器代码中的 'get'、'post' 和 'put' 操作如下所示:
app.get('/', function (req, res) {
db.collection('articles').find().toArray(function (err, result) {
if (err) return console.log(err);
res.render('index.ejs', {articles: result});
});
});
app.post('/articles', function (req, res) {
db.collection('articles').save(req.body, function (err, result) {
if (err) return console.log(err);
console.log('saved to database');
res.redirect('/');
});
});
app.put('/articles', function (req, res) {
db.collection('articles')
.findOneAndUpdate({title: 'test title'}, {
$set: {
title: req.body.title,
body: req.body.body
}
}, {
sort: {_id: -1},
upsert: true
}, function (err, result) {
if (err) return res.send(err);
res.send(result);
});
});
我想消除这个 404 错误,这样我就可以继续使用 'get' 请求的最终原因:用以前发布的博客条目的内容填充文本字段。完成编辑后,我计划使用 'put' 请求来完成 MongoDB 中的更改。
如果您需要任何其他信息,请告诉我。非常感谢您的建议!
您没有 GET /articles
路线;但是,您确实有一条 GET /
路线。
如果你想让GET /articles
起作用,你应该把app.get
的第一个参数改成'/articles'
。
我正在使用我学到的原则构建一个简单的博客 here(使用 Express 和 MongoDB 的 CRUD 应用程序)。为了编辑以前发布的 post,我想用博客 post 的内容填充一个文本字段,该博客的标题是从 drop-down 菜单中选择的。这样我就可以轻松地进行更改并提交它们。
我写了一个基本的 'put' 获取请求,它除了在出现问题时报告错误外什么都不做:
fetch('articles', {method: 'put'})
.then(
function(response) {
if (response.status !== 200) {
console.log('Looks like there was a problem. Status Code: ' + response.status);
return;
}
// Examine the text in the response
response.json().then(function(data) {
console.log(data);
});
}
)
.catch(function(err) {
console.log('Fetch Error :-S', err);
});
这里没有出错,所以console.log(data)
被执行了:
但是,当我将 'put' 更改为 'get'(并且不进行其他更改)时,出现 404 错误:
为什么'get' fetch 请求会导致404 错误,而'put' 请求不会?我在本地托管此应用程序,所以我 运行 来自 localhost:3000。我的服务器代码中的 'get'、'post' 和 'put' 操作如下所示:
app.get('/', function (req, res) {
db.collection('articles').find().toArray(function (err, result) {
if (err) return console.log(err);
res.render('index.ejs', {articles: result});
});
});
app.post('/articles', function (req, res) {
db.collection('articles').save(req.body, function (err, result) {
if (err) return console.log(err);
console.log('saved to database');
res.redirect('/');
});
});
app.put('/articles', function (req, res) {
db.collection('articles')
.findOneAndUpdate({title: 'test title'}, {
$set: {
title: req.body.title,
body: req.body.body
}
}, {
sort: {_id: -1},
upsert: true
}, function (err, result) {
if (err) return res.send(err);
res.send(result);
});
});
我想消除这个 404 错误,这样我就可以继续使用 'get' 请求的最终原因:用以前发布的博客条目的内容填充文本字段。完成编辑后,我计划使用 'put' 请求来完成 MongoDB 中的更改。
如果您需要任何其他信息,请告诉我。非常感谢您的建议!
您没有 GET /articles
路线;但是,您确实有一条 GET /
路线。
如果你想让GET /articles
起作用,你应该把app.get
的第一个参数改成'/articles'
。