I would like to run a website developed with node.js in local. I already installed node.js but when I lauch a .js file on my terminal, nothing happen ( $ node file.js ) Also, I guess I have to simulate a server ? How can I do that with node?
I would like to run a website developed with node.js in local. I already installed node.js but when I lauch a .js file on my terminal, nothing happen ( $ node file.js ) Also, I guess I have to simulate a server ? How can I do that with node?
Share Improve this question edited Apr 24, 2017 at 12:07 Cœur 38.8k25 gold badges205 silver badges277 bronze badges asked Oct 25, 2015 at 16:53 MichaëlMichaël 6192 gold badges8 silver badges30 bronze badges 2- We can't really help you unless you show us your code and explain much more specifically what you're trying to acplish. node.js is just a Javascript run-time environment. It doesn't start a web server by default. You can add code to start a web server. It sounds like perhaps you need to find and follow a tutorial on setting up a web site with node.js and Express. – jfriend00 Commented Oct 25, 2015 at 18:18
- If, this is really just a general question for "how to I make a web site with node.js", then that is far too broad for Stack Overflow. Instead, you should do some Google research on your own (as there are thousands of tutorials out there on this topic) and then post a question when you have specific code running and you got stuck on some particular issue. – jfriend00 Commented Oct 25, 2015 at 18:24
2 Answers
Reset to default 1You can start a simple server with the example that can be found on nodejs:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
https://nodejs/en/about/
To develop a website it is very helpful to use a web framework such as Express.
http://expressjs./
You should use:
npm start file.js
but also be sure to check out nodemon, which is very helpful for debugging - it restarts your app on code change.
Also be sure to check out the express generator, which will set up a node+express app that you can check out to figure how to get the server and routes going.