当前位置 : 主页 > 网页制作 > Nodejs >

node.js bodyParser错误

来源:互联网 收集:自由互联 发布时间:2021-06-16
我正在按照一个教程来设置node.js服务器和mongoDB数据库.我正处于需要启动npm的位置,但这是它返回的错误 C:\Users\Jay\IdeaProjects\nodetest1npm start application-name@0.0.1 start C:\Users\Jay\IdeaProjects\nod
我正在按照一个教程来设置node.js服务器和mongoDB数据库.我正处于需要启动npm的位置,但这是它返回的错误

C:\Users\Jay\IdeaProjects\nodetest1>npm start

> application-name@0.0.1 start C:\Users\Jay\IdeaProjects\nodetest1
> node ./bin/www


    module.js:340
        throw err;
              ^
    Error: Cannot find module 'bodyParser'
        at Function.Module._resolveFilename (module.js:338:15)
        at Function.Module._load (module.js:280:25)
        at Module.require (module.js:364:17)
        at require (module.js:380:17)
        at Object.<anonymous> (C:\Users\Jay\IdeaProjects\nodetest1\app.js:6:18)
        at Module._compile (module.js:456:26)
        at Object.Module._extensions..js (module.js:474:10)
        at Module.load (module.js:356:32)
        at Function.Module._load (module.js:312:12)
        at Module.require (module.js:364:17)

npm ERR! application-name@0.0.1 start: `node ./bin/www`
npm ERR! Exit status 8
npm ERR!
npm ERR! Failed at the application-name@0.0.1 start script.
npm ERR! This is most likely a problem with the application-name package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node ./bin/www
npm ERR! You can get their info via:
npm ERR!     npm owner ls application-name
npm ERR! There is likely additional logging output above.
npm ERR! System Windows_NT 6.2.9200
npm ERR! command "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! cwd C:\Users\Jay\IdeaProjects\nodetest1
npm ERR! node -v v0.10.28
npm ERR! npm -v 1.4.9
npm ERR! code ELIFECYCLE
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     C:\Users\Jay\IdeaProjects\nodetest1\npm-debug.log
npm ERR! not ok code 0

我已经完全遵循了教程,对node / mongo来说还是新手,所以如果有人知道如何提供帮助,我会很感激.

这是我的app.js代码

var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('bodyParser');

//Database
var mongo = require('mongoskin');
var db = mongo.db("mongodb://localhost:27017/nodetest1", {native_parser:true});

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

//view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

//Make our db accessible to our router
app.use(function(req,res,next){
    req.db = db;
    next();
});

app.use('/', routes);
app.use('/users', users);

///Catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

///error handlers

//development error handler
//will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: {}
        });
    });
}

//production error handler
//no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});

module.exports = app;
看起来您需要安装节点模块. npm install -d应该安装所有依赖项.

你的bodyParser也是错的.它应该是:

var bodyParser = require('body-parser')

见:body parser API

网友评论