首页 > 解决方案 > 运行节点服务器时出现 404

问题描述

嗨学习使用节点和其他后端组件并在运行一个简单的输入表单时遇到这个 404:

Not Found
404

NotFoundError: Not Found
    at /Users/samgniel/Desktop/saas-tutorial/app.js:25:8
    at Layer.handle [as handle_request] (/Users/samgniel/Desktop/saas- 
tutorial/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/samgniel/Desktop/saas- 
tutorial/node_modules/express/lib/router/index.js:317:13)
    at /Users/samgniel/Desktop/saas-tutorial/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/samgniel/Desktop/saas- 
tutorial/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/samgniel/Desktop/saas- 
tutorial/node_modules/express/lib/router/index.js:275:10)
    at SendStream.error (/Users/samgniel/Desktop/saas-tutorial/node_modules/serve- 
static/index.js:121:7)
    at SendStream.emit (events.js:210:5)
    at SendStream.error (/Users/samgniel/Desktop/saas- 
tutorial/node_modules/send/index.js:270:17)
    at SendStream.onStatError (/Users/samgniel/Desktop/saas- 
tutorial/node_modules/send/index.js:421:12)

输入表单的代码是:

<form action="/signup" method="post">
  <input required type="email" name="email" id="emailForm" placeholder="Your Email">
  <input type="password" name="password" id="passwordForm" placeholder="Password">
  <input type="submit" name="" id="" value="Signup!">
</form>

.js 调用函数是:

app.post('/signup', function(req, res, next) {
console.log(req.body);
});

帮助解决此错误将不胜感激:)

标签: javascriptnode.jshttp-status-code-404ejs

解决方案


您似乎没有向客户返回任何响应。您可以纠正的一种方法是使用 express 提供的 res 对象中的 send 函数。

app.post('/signup', function(req, res, next) {
    console.log(req.body);
    res.send('Hello World') // this can also take an object as response. res.send({ msg: 'hello world' })
});

重新调整这条线也可能会有所帮助ok comment out this line app.use(function(req, res, next) { next(createError(404)); });

希望这可以帮助。


推荐阅读