我正在使用网络服务而且我使用的是npm服务器和mongodb(我不知道这是否相关,但我想提供完整的背景)我像往常一样启动服务器并且突然npm服务器停止工作(我不知道它是否相关,但是当我运行一个脚本写入DB写入数据库然后粉碎时发生了这种情况)当我尝试再次启动它时会显示此错误消息:< / p>
foo.post('/register', function(req, res) {
^
TypeError: Object #<Object> has no method 'post'
at Object.<anonymous> (C:\work\foo\lib\foo.js:19:5)
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)
at require (module.js:380:17)
at Object.<anonymous> (C:\work\foo\routes\foo.js:4:11)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
npm ERR! foo@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 8
npm ERR!
npm ERR! Failed at the foo@0.0.0 start script.
npm ERR! This is most likely a problem with the foo 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 foo
npm ERR! There is likely additional logging output above.
npm ERR! System Windows_NT 6.2.9200
npm ERR! command "C:\\mobile\\\\node.exe" "C:\\mobile\\node_modules\\npm\\bin\\n
pm-cli.js" "start"
npm ERR! cwd C:\work\foo
npm ERR! node -v v0.10.31
npm ERR! npm -v 1.4.23
npm ERR! code ELIFECYCLE
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! C:\work\foo\npm-debug.log
npm ERR! not ok code 0
LIB \ foo.js:
//var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/foodb');
var userList = 'tmp';
//var express = require('express');
//var router = express.Router();
var foo = {};
foo.getData = function(req, res){
debugger
var collection = db.get(userList);
collection.findOne().on('success', function (doc) {
res.send(doc);
});
};
/* POST to Add User Service */
foo.post('/register', function(req, res) {
// Set our internal DB variable
// var db = req.db;
// Get our form values. These rely on the "name" attributes
var userName = document.forms["registration"]["username"].value;
var userEmail = document.forms["registration"]["email"].value;
var passWord = document.forms["registration"]["passid"].value;
// Set our collection
var collection = db.get(userList);
// Submit to the DB
collection.insert({
"username" : userName,
"email" : userEmail,
"password" : passWord
}, function (err, doc) {
if (err) {
// If it failed, return error
res.send("There was a problem adding the information to the database.");
}
else {
// If it worked, set the header so the address bar doesn't still say /adduser
res.location("myfoo.html");
// And forward to success page
res.redirect("myfoo.html");
}
});
});
module.exports = foo;
路由\ foo.js:
var express = require('express');
var router = express.Router();
var foo = require('../lib/foo.js');
/* GET home page. */
router.get('/getData', foo.getData);
router.post('/getData', foo.getData);
router.get('/register',foo.writeUser);
router.post('/register',foo.writeUser);
module.exports = router;
的package.json:
{
"name": "foo",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"debug": "node-debug ./bin/www"
},
"dependencies": {
"body-parser": "1.8.1",
"cookie-parser": "1.3.3",
"debug": "2.0.0",
"express": "4.9.0",
"jade": "1.6.0",
"mongodb": "1.4.19",
"monk": "0.9.0",
"morgan": "1.3.0",
"serve-favicon": "2.1.3"
}
}
app.js:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
//require routers
var foo = require('./routes/foo.js');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
//create public folder
app.use(express.static(path.join(__dirname, 'public')));
// app use routers
app.use('/foo',foo);
// 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
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;