eclipse - nodeJS 应用程序错误

标签 eclipse node.js express

我是 NodeJS 的新手。我正在尝试在 Eclipse 中构建一个民意调查应用程序。当我创建“民意调查”项目时,“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');

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');

// 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());
app.use(express.static(path.join(__dirname, 'public')));

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: err
        });
    });
}

// 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;

当我将“App.js”作为 Node 应用程序运行时。这给出了以下 o/p:

module.js:340
    throw err;
          ^
Error: Cannot find module 'serve-favicon'
    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> (/home/surya/workspace/Polls/app.js:3:15)
    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 Function.Module.runMain (module.js:497:10)

我已经安装了 Express。在我关注的书中,他们说当我将它作为 Node 应用程序运行时,它会导致浏览器并将其导航到 localhost:3000。但是当我这样做时它显示空白页面。如何使这段代码工作?

最佳答案

当您第一次通过 express generator 创建 express 应用程序时,您需要安装依赖项

因此,首先使用 npm install

安装所有必需的依赖项(包括 serve-favicon 依赖项之一)

关于eclipse - nodeJS 应用程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27888347/

相关文章:

javascript - 如何使用 Express.js 中的值数组和准备好的语句来更新 MySQL 中的表?

mysql - 如何将数组传递给mysql查询

node.js - 尽管用户在网站上注销,但 Socket.io 授权仍然存在

c - 无法将 libusb 链接到 Mac 上的 C 项目

java - Android Facebook 崩溃

java - Eclipse & Dark Juno - 有什么方法可以让控制台和其他功能变暗吗?

eclipse - JUnit + Maven + Eclipse : Why @BeforeClass does not work?

node.js - 如何在离线时安装 npm 包?

node.js - HTTP/2 在 AWS API Gateway + Lambda 上推送

node.js - react Node 通过 axios 传递文件,出现 multer 错误 500