1

我是 node js 的新手,并且正在学习 scotch.io上的教程。我已经导入 morgan 来记录请求,但是当我运行代码时,我得到TypeError: app.use is not a function。这是我的 app.js 代码;

const express = require('express');
const logger = require('morgan');
const bodyParser = require('body-parser');

const app = express;

app.use(logger('dev'));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.get('*', (req, res) => res.status(200).send({
    message: 'Welcome to deep thinking.'
}));

module.exports = app;

对于 package.json:

{
  "name": "postgres-express-react-node-tutorial",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start:dev": "nodemon ./bin/www",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.18.3",
    "express": "^4.16.3",
    "morgan": "^1.9.1"
  },
  "devDependencies": {
    "nodemon": "^1.18.4"
  }
}
4

2 回答 2

5

require('express')返回一个函数,您应该调用该函数以获取快速应用程序。所以:

const app = express;

应改为:

const app = express();
于 2018-10-08T05:06:00.203 回答
2

试试这个,换app=express; to app=express();

const express = require('express');
const logger = require('morgan');
const bodyParser = require('body-parser');

const app = express(); // changed this line

app.use(logger('dev'));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.get('*', (req, res) => res.status(200).send({
    message: 'Welcome to deep thinking.'
}));

module.exports = app;
于 2018-10-08T05:00:37.437 回答