npm start vs node app.js [英] npm start vs node app.js

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

问题描述

我对Node非常陌生,并试图了解应用基础知识。我很好奇为什么这两个命令:

I'm extremely new to Node and trying to get my head around app basics. I'm curious as to why these two commands:


node app.js

node app.js

- vs -

--vs--


npm start

npm start

向控制台输出相同的内容并且似乎继续监听,但为什么当我尝试访问 http:// localhost:3000 我只在运行第一个命令时得到404.

output the same thing to the console and appear to continue "listening", but why when I try to access http://localhost:3000 I get a 404 only when running the first command.

我看到Express 4似乎有不同应用程序结构,但为什么一个成功侦听而另一个没有,尽管在控制台中有相同的行为?

I see that Express 4 seems to have a different app structure, but why is it that one successfully listens and the other doesn't, despite the same behavior in the console?

任何解释都是有帮助的。谢谢!

Any explanation is helpful. Thanks!

推荐答案

这两个命令不一定相同。 npm start 运行'start'脚本配置所说的运行,如'package.json'中所定义的那样运行,节点app.js 在'node'中执行'app.js'文件。有关详细信息,请参见 http://browsenpm.org/package.json 。所以,如果您有以下package.json,那么命令就完全不同了。

The two of these commands aren't necessarily the same. npm start runs whatever the 'start' script config says to run as defined in your 'package.json', node app.js executes the 'app.js' file in 'node'. See http://browsenpm.org/package.json for more info. So if you had the following package.json then the commands are completely different.

{
    "name": "my cool node project",
    ....
    "scripts": {
        "start": "node index.js"
    }
    ....
}

以下package.json是你想让它们相同的东西。

The following package.json is what you'll want to make them identical.

{
    "name": "my cool node project",
    ....
    "scripts": {
        "start": "node app.js"
    }
    ....
}

我首先检查启动脚本设置为运行的内容并尝试直接在CLI中运行相同的命令,而不是通过NPM查看区别在于。

I'd start by checking what the 'start' script is set to run and try running the same command directly in your CLI rather than through NPM to see where the difference is.


但为什么一个成功收听而另一个不成功

but why is it that one successfully listens and the other doesn't

如果服务器返回404,则表明服务器正在侦听,但是文件根目录或访问权限未正确设置,因此它返回未找到文件响应。

If the server is returning a 404 this would suggest the server is listening, but either the document root or access permissions aren't being setup properly so it returns a 'File not Found' response.

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

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