在创建一个简单的服务器并尝试测试来自虚拟路由的响应时,当我在chrome中运行它时,浏览器会无限旋转,并且不会加载页面。
index.js
const express = require('express');
const bodyParse = require('body-parser');
const cors = require('cors');
const morgan = require('morgan');
const index = express();
// log formatter
index.use(morgan('combine'));
index.use(bodyParse.json);
index.use(cors());
index.get('/status', (req, res) => {
res.send({
message: 'herrrooo'
})
});
index.listen(process.env.PORT || 5000);
package.json
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon index.js",
"dev": "concurrently \"npm run start\" "
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.3",
"cors": "^2.8.4",
"express": "^4.16.3",
"morgan": "^1.9.0",
"nodemon": "^1.17.5"
},
"devDependencies": {
"@types/express": "^4.11.1",
"concurrently": "^3.5.1",
"eslint": "^4.19.1",
"eslint-config-standard": "^11.0.0",
"eslint-plugin-import": "^2.12.0",
"eslint-plugin-node": "^6.0.1",
"eslint-plugin-promise": "^3.8.0",
"eslint-plugin-standard": "^3.1.0"
}
}
我做错了什么??
发布于 2018-06-04 04:56:15
更改此设置:
index.use(bodyParse.json);
要这样做:
index.use(bodyParse.json());
您没有传递正确的中间件函数,所以express在到达该中间件时被卡住了,因为中间件从未调用next()
来继续下一步的请求处理。
https://stackoverflow.com/questions/50670862
复制相似问题