全新的Node并跟随此MEAN app tutorial并在Linux中的CL中执行'node server'时遇到错误(我一直在使用sudo到mkdir并触摸所有文件,然后运行节点服务器)。 。
>sudo node server
/home/jake/web/tasker/node_modules/express/lib/router/index.js:458
throw new TypeError('Router.use() requires middleware function but got a ' + gettype(fn));
^
TypeError: Router.use() requires middleware function but got a Object
at Function.use (/home/jake/web/tasker/node_modules/express/lib/router/index.js:458:13)
at EventEmitter.<anonymous> (/home/jake/web/tasker/node_modules/express/lib/application.js:220:21)
at Array.forEach (native)
at EventEmitter.use (/home/jake/web/tasker/node_modules/express/lib/application.js:217:7)
at Object.<anonymous> (/home/jake/web/tasker/server.js:23:5)
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 Function.Module.runMain (module.js:442:10)
我的主server.js文件:
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var index = require('./routes/index');
var tasks = require('./routes/tasks');
var port = 3000;
var app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine','ejs');
app.engine('html',require('ejs').renderFile);
//static folder for Angular stuff
app.use(express.static(path.join(__dirname, 'client')));
//body Parser MiddleWare
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use('/', index);
app.use('/api', tasks);
app.listen(port, function(){
console.log('Server has begun');
});
和package.json:
{
"name": "tasker",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.17.1",
"ejs": "^2.5.6",
"express": "^4.15.2",
"mongojs": "^2.4.0"
}
}
答案 0 :(得分:0)
我最后重做了教程,除了通过命令行创建文件夹和文件,我只是右键单击,现在它可以正常工作。所以它可能与sudo权限有关,或者我第二次做这件事时根本就没有写错了。很抱歉没有回答。