节点服务器无响应

时间:2020-10-24 04:13:06

标签: node.js mongodb express mongoose mean-stack

我无法从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上启动 数据库已连接

然后在浏览器的快照中。 elsewhere

这是邮递员的快照。 Snapshot of localhost:3000 running on Chrome

以前它正在运行,但由于最近几天没有响应。

1 个答案:

答案 0 :(得分:0)

尝试一下

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()