我刚开始使用node和express,并试图发出一个发布请求并将结果立即推送到数组中。将来,我将向数据库发出这些请求,但出于学习目的,我现在对阵列进行此操作。获得回复有效,发布请求使我在邮递员中出错。
const express = require('express');
const app = express();
const port = 5000;
const arr = [];
app.use(express.json());
app.post('/' , (req,res) => {
arr.push(req.body);
res.send(arr);
})
app.listen(port, () => {console.log(`listening on port ${port}`)});
可悲的是,邮递员的错误并没有告诉我很多。
错误:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /</pre>
</body>
</html>
请求:
{
"app" : "Post"
}
requeSt的规格:
connection →keep-alive
content-length →140
content-security-policy →default-src 'none'
content-type →text/html; charset=utf-8
date →Fri, 23 Aug 2019 07:55:32 GMT
x-content-type-options →nosniff
x-powered-by →Express