节点服务器没有响应
Node Server is not Responding
我无法从 Localhost:3000 获得任何回复。我是那个端口上的 运行 节点,它成功连接,即使数据库已连接,但节点没有给出任何响应。
这是我的代码..
Package.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"
}
}
mongoose.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'));
通过节点 server.js 后,以下输出出现..
服务器在端口 3000 上启动
数据库已连接
在浏览器的快照中..
Snapshot of localhost:3000 running on Chrome
这是邮递员的快照..
Snapshot from Postman
以前是 运行,但最近几天没有响应。
试试这个
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()
错过了。
我无法从 Localhost:3000 获得任何回复。我是那个端口上的 运行 节点,它成功连接,即使数据库已连接,但节点没有给出任何响应。
这是我的代码..
Package.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"
}
}
mongoose.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'));
通过节点 server.js 后,以下输出出现.. 服务器在端口 3000 上启动 数据库已连接
在浏览器的快照中.. Snapshot of localhost:3000 running on Chrome
这是邮递员的快照.. Snapshot from Postman
以前是 运行,但最近几天没有响应。
试试这个
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()
错过了。