节点服务器没有响应
Posted
技术标签:
【中文标题】节点服务器没有响应【英文标题】:Node Server is not Responding 【发布时间】:2021-02-07 02:28:18 【问题描述】:我无法从 Localhost:3000 获得任何响应。我在该端口上运行节点,即使连接了数据库,它也成功连接,但节点没有给出任何响应。
这是我的代码..
包.json
"name": "tax-engine",
"version": "0.0.0",
"scripts":
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
,
"private": true,
"dependencies":
"@angular/animations": "~10.1.0",
"@angular/common": "~10.1.0",
"@angular/compiler": "~10.1.0",
"@angular/core": "~10.1.0",
"@angular/forms": "~10.1.0",
"@angular/material": "^10.2.4",
"@angular/platform-browser": "~10.1.0",
"@angular/platform-browser-dynamic": "~10.1.0",
"@angular/router": "~10.1.0",
"angular-material-fileupload": "^3.0.1",
"express": "^4.17.1",
"mongodb": "^3.6.2",
"mongoose": "^5.10.9",
"multer": "^1.4.2",
"rxjs": "~6.6.0",
"tslib": "^2.0.0",
"zone.js": "~0.10.2"
,
"devDependencies":
"@angular-devkit/build-angular": "~0.1001.0",
"@angular/cli": "~10.1.0",
"@angular/compiler-cli": "~10.1.0",
"@types/node": "^12.11.1",
"@types/jasmine": "~3.5.0",
"@types/jasminewd2": "~2.0.3",
"codelyzer": "^6.0.0",
"jasmine-core": "~3.6.0",
"jasmine-spec-reporter": "~5.0.0",
"karma": "~5.0.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage-istanbul-reporter": "~3.0.2",
"karma-jasmine": "~4.0.0",
"karma-jasmine-html-reporter": "^1.5.0",
"protractor": "~7.0.0",
"ts-node": "~8.3.0",
"tslint": "~6.1.0",
"typescript": "~4.0.2"
猫鼬.js
const error = require('console');
const Module = require('module');
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://127.0.0.1:27017/test', useNewUrlParser: true, useUnifiedTopology: true )
.then(() => console.log("Database Connected"))
.catch((error) => console.log("error"));
module.exports = mongoose;
server.js
// const express = require('express');
// const app = express();
// const mongoose = require('./src/app/database/mongoose')
// /*CORS Cross Origin Request Security*/
// app.use((req, res, next) =>
// res.header("Access-Control-Allow-Origin", "*");
// res.header("Access-Control-Allow-Methods", "GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE");
// res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
// );
// app.get('/', (req, res) => res.send('Hello World!'));
// app.use(express.json());
// app.listen(3000, () => console.log(`Example app listening on port 3000`));
// call all the required packages
const express = require('express')
const bodyParser= require('body-parser')
const multer = require('multer');
const mongoose = require('./src/app/database/mongoose')
//CREATE EXPRESS APP
const app = express();
const postmodel = require('./src/app/database/models/post')
app.use(express.json());
/*CORS Cross Origin Request Security*/
app.use((req, res, next) =>
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
);
app.get('/getall', (req, res) =>
post.find()
.then(lists => res.send(lists))
.catch((error) => console.log(error));
);
app.post("/Posts",(req, res, next)=>
const post = new postmodel(
title: req.body.title,
content: req.body.content
);
post.save();
console.log(post);
res.status(201).json(
message : 'Post Added Successfully'
);
)
app.get("/getPosts", (req, res, next)=>
postmodel.find()
.then((documents)=>
console.log(documents);
);
res.status(200).json(
message: 'Posts Fetched Successfully',
posts: posts
);
);
//app.use(bodyParser.urlencoded(extended: true))
//ROUTES WILL GO HERE
app.get('/', function(req, res)
res.json( message: 'WELCOME' );
);
app.listen(3000, () => console.log('Server started on port 3000'));
通过 node server.js 后,输出如下.. 服务器在 3000 端口启动 已连接数据库
在浏览器的快照中.. Snapshot of localhost:3000 running on Chrome
这是邮递员的快照.. Snapshot from Postman
之前它正在运行,但最近几天它没有响应。
【问题讨论】:
@santimirandarp 是的 mongodb 正在运行.. 你要 tsconfig.json 文件吗? 你检查了 chrome devtools 看看你是否得到了 200 级的响应? @santimirandarp 我去了 mongo 核心文件夹,从那里我进入命令提示符,然后我运行 mongod,然后运行 mongo,它成功运行了.. 是的,它与 mongo 没有太大关系跨度> @santimirandarp 尝试关闭 mongodb 但同样的问题仍然存在.. 没有任何路由响应。 可以分享一下服务器启动日志吗?您也可以使用 curl 尝试请求并共享其输出,您可以使用命令curl -X GET "http://localhost:3000"
执行相同操作。
【参考方案1】:
试试这个
const express = require('express')
const bodyParser= require('body-parser')
const multer = require('multer');
const mongoose = require('./src/app/database/mongoose')
//CREATE EXPRESS APP
const app = express();
const postmodel = require('./src/app/database/models/post')
app.use(express.json());
/*CORS Cross Origin Request Security*/
app.use((req, res, next) =>
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next()
);
app.get('/getall', (req, res) =>
post.find()
.then(lists => res.send(lists))
.catch((error) => console.log(error));
);
app.post("/Posts",(req, res, next)=>
const post = new postmodel(
title: req.body.title,
content: req.body.content
);
post.save();
console.log(post);
res.status(201).json(
message : 'Post Added Successfully'
);
)
app.get("/getPosts", (req, res, next)=>
postmodel.find()
.then((documents)=>
console.log(documents);
);
res.status(200).json(
message: 'Posts Fetched Successfully',
posts: posts
);
);
//app.use(bodyParser.urlencoded(extended: true))
//ROUTES WILL GO HERE
app.get('/', function(req, res)
res.json( message: 'WELCOME' );
);
app.listen(3000, () => console.log('Server started on port 3000'));
这就是我改变的
app.use((req, res, next) =>
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next()
);
编辑
代码可以正常工作,但现在我相信它已修复。错过了一个next()
。
【讨论】:
感谢您的回答。这件事奏效了。你能解释一下你做了什么吗? 能否请您删除与问题相同的代码并添加更改的部分?并请为您的答案添加解释。 @shaishavshah 代码挂在中间件 AFAIK 上。但是你也应该包括next()
,就像我现在在代码中所做的那样。
@Minsky 明白了,谢谢你的帮助。以上是关于节点服务器没有响应的主要内容,如果未能解决你的问题,请参考以下文章