错误:无法在server.js中找到模块'res'

时间:2016-02-08 00:03:50

标签: node.js

我正在尝试输出“hello world”文本。我正在使用expressjs并使用名为“ejs”的视图引擎。我是nodeJS的初学者

var express = require("express");
var app = express();
var path = require('path');
var res = require("res");

var port = process.env.PORT;
app.set('view engine');
app.set('views',path.resolve(__dirname,'client','views'));

app.get('/',function() {
    res.render('index.ejs');
});

app.listen(port,function(){
    console.log('Server running in PORT '+port);
});

这是错误:

module.js:338
    throw err;
    ^

Error: Cannot find module 'res'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:286:25)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object. (/home/ubuntu/workspace/server.js:4:11)
    at Module._compile (module.js:434:26)
    at Object.Module._extensions..js (module.js:452:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.runMain [as _onTimeout] (module.js:475:10)


Process exited with code: 1

这里有什么问题?

1 个答案:

答案 0 :(得分:2)

res是路由处理程序的函数参数,它不是require()的模块。

请改为尝试:

var express = require("express");
var app = express();
var path = require('path');

var port = process.env.PORT;
app.set('view engine');
app.set('views',path.resolve(__dirname,'client','views'));

app.get('/',function(req, res) {
    res.render('index.ejs');
});

app.listen(port,function(){
    console.log('Server running in PORT '+port);
});