[英]Cannot post using express on node.js
I am trying to make a POST route for adding filenames, but I keep getting:我正在尝试为添加文件名创建 POST 路由,但我不断收到:
Cannot GET /api/data?file=example.fasta
无法获取 /api/data?file=example.fasta
error when I trying testing the route.尝试测试路线时出错。 Please could you help me fix this issue.
请你帮我解决这个问题。
Here is the code for my router.js:这是我的 router.js 的代码:
const fs = require('fs');
const express = require('express');
const bodyParser = require('body-parser');
const parser = require('parser');
const router = express.Router();
router.use(bodyParser.json());
router.use(bodyParser.urlencoded({ extended: false }));
// middleware
router.use(function (req, res, next) {
console.log('Received request');
// hand over route to specific route handler
next();
});
router.post('/data', function (req, res) {
if (!req.body.file.toString().endsWith('.fasta')) {
res.status(400).json({ message: "Bad Request" });
} else {
parser.parseFile(req.body.filename)
res.json({ message: "File parsed and data entered.", location: "/data/" });
}
});
server.js服务器.js
const express = require('express');
// server
const app = express();
const port = 3000;
app.listen(port, function () {
console.log(`Server running on port ${port}`)
});
// import router
const router = require('./router');
app.use('/api', router)
Try using /api/data?file=example.fasta
instead of /api/data/file=example.fasta
.尝试使用
/api/data?file=example.fasta
而不是/api/data/file=example.fasta
。 As the?, stands for a url query, on the other hand / is routing to somewhere else, another page for example.作为?,代表 url 查询,另一方面 / 正在路由到其他地方,例如另一个页面。 with / you would go to a page named "file=example.fasta", but with?, you are telling the code that you want a file name example.fasta.
使用 / 你会 go 到一个名为“file=example.fasta”的页面,但是使用?,你告诉代码你想要一个文件名 example.fasta。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.