javascript - Node Js TypeError with morgan: app.use is not a function - Stack Overflow

I am new to node js and following a tutorial on scotch.io. I've imported morgan for logging reques

I am new to node js and following a tutorial on scotch.io. I've imported morgan for logging requests, but when I run the code I get TypeError: app.use is not a function. This is my code for 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: 'Wele to deep thinking.'
}));

module.exports = app;

And for 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"
  }
}

I am new to node js and following a tutorial on scotch.io. I've imported morgan for logging requests, but when I run the code I get TypeError: app.use is not a function. This is my code for 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: 'Wele to deep thinking.'
}));

module.exports = app;

And for 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"
  }
}
Share Improve this question asked Oct 8, 2018 at 4:56 Ebenezer DonEbenezer Don 231 silver badge4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

require('express') returns a function, which you should call in order to get the express application. Therefore:

const app = express;

should be changed to:

const app = express();

Try this, change 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: 'Wele to deep thinking.'
}));

module.exports = app;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744361164a4570478.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信