node.js - 新手 请教“TypeError: express is not a function” .

查看:2225
本文介绍了node.js - 新手 请教“TypeError: express is not a function” .的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

我是新手,在网上看到一个node.js的教程,初始化一个项目,跟着教程打下来,代码启动总是提示错误:

D:\nodejs\cms\app.js:3
var app = express();
          ^

TypeError: express is not a function
    at Object.<anonymous> (D:\nodejs\cms\app.js:3:11)
    at Module._compile (module.js:410:26)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Module.require (module.js:354:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (D:\nodejs\cms\bin\www:3:11)
    at Module._compile (module.js:410:26)
    at Object.Module._extensions..js (module.js:417:10) 

app.js代码如下:

var express = require('./config/express');
var app = express();
module.exports = app;

/config/express.js代码如下:

var express = require('express');
var bodyParser = require('body-parser');
module.express = function(){
    console.log('init express...');
    var app = express();
    app.use(bodyParser.json());
    app.use(function(req, res, next){
        res.status(404);
        try {
            return res.json('Not found');
        } catch(e) {
            console.error('404 set header after sent');
        }
    });
    return app;
}; 

bin/www代码如下:

#!/usr/bin/env node
var app = require('../app');
var config = require('../config/config');
app.listen(config.port, function(){
    console.log('app started, listening on port:' + config.port);
});

package.json :

{
  "name": "cms",
  "version": "1.0.0",
  "description": "demo app",
  "main": "bin/www",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "abc",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.15.0",
    "express": "^4.13.4"
  }
}

请麻烦帮忙看一下, 谢谢。

解决方案

config/express.js 中

module.express

改成

module.exports

这篇关于node.js - 新手 请教“TypeError: express is not a function” .的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆