gpt4 book ai didi

node.js bodyParser 错误

转载 作者:可可西里 更新时间:2023-11-01 09:19:23 26 4
gpt4 key购买 nike

我正在学习如何设置 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;

最佳答案

看起来您需要安装 Node 模块。 npm install -d 应该安装所有依赖项。

你的 bodyParser 也是错误的。应该是:

var bodyParser = require('body-parser')

参见:body parser API

关于node.js bodyParser 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23656862/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com