使用app.js或"npm start"启动Express.js? [英] Starting Express.js Using app.js or 'npm start'?

查看:136
本文介绍了使用app.js或"npm start"启动Express.js?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在本地服务器上安装了Node.js,当我创建一个"server.js"文件(使用.createServer()方法添加服务器)时,它可以正常加载.

I've recently installed Node.js on a local server and when I create a 'server.js' file (adding a server using the .createServer() method), it loads fine.

但是,在安装Express.js之后,默认文件如下:

However after installing Express.js, the default files are as follows:

/bin
/node_modules
/public
/routes
/views
app.js
package.json

在阅读了一些文档之后,我被指示转到终端并输入以下命令:

After following some documentation, I am instructed to go to Terminal and enter the following command:

node app.js

什么也没发生,命令行在不到一秒钟的时间内刷新到下一行,并且在访问了正确的IP和端口后打开浏览器,无济于事.

To which nothing happens, the command line refreshes to the next line in less than a second, and opening a browser after visiting the proper IP and port, to no avail.

下面是app.js文件中的代码:

Below is the code inside of the app.js file:

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(path.join(__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;

我知道在文件开头需要的实际表达"模块是发生魔术的地方,但是,当我运行命令行时:

I understand that the actual 'express' module that is being required in the beginning of the file is where the magic happens, however, when I run the command line:

node app.js

什么都没发生.但是,如果我拨打以下行:

Nothing happens. However, if I call the following line:

npm start

一切似乎都还好.我想按原样使用文档,为什么app.js无法正常工作?

Everything appears to be okay. I would like to follow the documentation as is, any reason why the app.js wouldn't be working?

谢谢.

更新:我的问题与已经发布的另一个问题太相似,因此我必须明确说明它们的不同之处.在另一个问题中,有人在质疑在默认的"app.js"文件上运行Supervisor命令时收到的代码.

UPDATE: My question was too similar to another one already posted, so I must clarify exactly how they were different. In the other question, a person was questioning a code number they received while running the Supervisor command on the default 'app.js' file.

尽管本质上相似,但这个问题仍然存在,因为那些使用我的相同方法感到困惑的人将通过使用"node app.js"来关注实际"app.js"文件的标识,而无需完全了解Supervisor实用程序.

While similar in nature, this question should remain, as those who are confused by using my same approach will focus on the identity of the actual 'app.js' file by using 'node app.js' without having full knowledge of the Supervisor utility.

致谢.

推荐答案

感谢大家的出色回答,因为它们确实使我了解了app.js文件的实际操作以及它在哪里得到的功能.感谢Matthew Bakaitis和Bjarni Leifsson的宝贵意见.

Thank you all for your great responses, as they really allowed me to understand what is actually going on with the app.js file and where it receives it's functionality. Thank you to both Matthew Bakaitis and Bjarni Leifsson for their great input.

我要继续回答我自己的问题的唯一原因是,尽管解释了app.js文件的性质,但实际上该如何复制从命令行调用'node app.js'命令,如下所示:复制我所关注的Node.js的书并没有得到明确的解决.

The only reason why I am going to go ahead and answer my own question is because while the nature of the app.js file was explained, exactly how to replicate calling the 'node app.js' command from the command line as to replicate a Node.js book that I was following wasn't implicitly addressed.

在使用特定短语以前的express.js版本中的app.js"搜索google之后,我偶然发现了Jilles Soeters的一篇很棒的文章,名为了解Express app.js":

After searching google with the specific phrase "app.js in previous express.js versions", I happened upon a great article by Jilles Soeters entitled "Understanding the Express app.js":

http://jilles.me/getting-the-express-app-js /

下面是对我有用的解决方案的摘录:

Below is the excerpt of the solution that worked for me:

我要介绍的文件是app.js,这是您的主要配置文件 快递应用.当我第一次打开app.js时,我感到困惑.我会保存 您会遇到进行研究的麻烦,只需在此处进行介绍即可.

The file I'm covering is app.js, the main configuration file for your Express app. When I first opened app.js it confused me. I will save you the trouble of doing research and just cover them here.

在执行任何操作之前,请将以下内容添加到您的app.js中

Before you do anything add the following to your app.js

app.listen(3000);

您需要具备此条件才能在以下位置实际打开您的应用程序: 浏览器.启动应用程序后,使用 127.0.0.1:3000 节点app.js)

You need that in order to be able to actual open your app in the browser. Go to 127.0.0.1:3000 after you've started your app (using node app.js)

完成此操作后,我便能够运行命令

After doing this, I was able to run the command

node app.js

我能够从Express安装的根目录中运行此命令,并继续进行我的Node.js书籍.

I was able to run this command from the root directory of the Express install and proceed with my Node.js book with no additional problems.

这篇关于使用app.js或"npm start"启动Express.js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆