javascript - 运行节点服务器时出现 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);
});
帮助解决此错误将不胜感激:)
解决方案
您似乎没有向客户返回任何响应。您可以纠正的一种方法是使用 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)); });
希望这可以帮助。
推荐阅读
- google-tag-manager - 我应该使用工作箱运行时缓存 staleWhileRevalidate 来缓存 gtm.js 吗?
- javascript - 播放多个音轨
- node.js - 如何返回无效缓冲区以测试 API 调用?
- javascript - 如何创建文本通道
- python - Z3 Solver() 中的约束大小
- javascript - Select2 下拉问题。如何将日期选择器中的日期填充到 select2 下拉列表中?
- python - 识别超过 N 行的 10 分钟窗口
- javascript - 如何在 Electron 应用程序的加载时间内防止光标上的沙漏?
- docker - 如何在高山容器中安装 Docker?
- python - 将字符串安全地转换为元组列表中的整数