Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

javascript - npm start vs node app.js

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

--vs--

npm start

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.

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"
    }
    ....
}

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

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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...