节点Js TypeError与morgan:app.use不是函数

时间:2018-10-08 04:56:18

标签: javascript node.js express typeerror morgan

我是Node js的新手,并且正在学习有关scotch.io的教程。我已经导入了morgan来记录请求,但是运行代码时,我得到 TypeError:app.use不是一个函数。这是我的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"
  }
}

2 个答案:

答案 0 :(得分:1)

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

const app = express;

应更改为:

const app = express();

答案 1 :(得分:0)

尝试一下,更改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;