I'm beginning to learn web technologies and programming and I'm setting up my own local webserver. I have HTTPD, PHP, Python, MySQL all up and running on Windows. Now I want to add Node.js to the mix. I installed the Windows 64 bit installer. Now how do I begin? I have a basic Hello World script in a test.js file. But when I access this file in the browser it only displays as plain text. It's not executed. How to execute a Node.js script on the server?
- 
                    `node /path/to/your/file.js` – Aer0 Feb 07 '17 at 14:41
- 
                    Sorry for my newbie. Ok. It works. But how do I deploy it to a Web Hosting server online? I don't suppose I log into the server and type 'node file.js' every time I want the server to run – Aardo Feb 07 '17 at 14:53
- 
                    Currently you've to type in that command. Manually or through a service/tool/whatever like Jenkins. There are a lot of SaaS provider where you can automatically deploy and start your services in a GUI. Take a tour through Google. – Aer0 Feb 07 '17 at 14:58
- 
                    I see. One more question. How about the port? I expect users to open my URL without specifing a designated port. Just www.domain.com i.e. How to run Node.js on a default port? – Aardo Feb 07 '17 at 15:02
- 
                    @Aardenon check my answer – Ahmed farag mostafa Feb 07 '17 at 15:04
- 
                    Possible duplicate: http://stackoverflow.com/questions/7947195/start-with-node-js-as-a-complete-server-side-newbie – Quentin Feb 07 '17 at 15:07
- 
                    @Aardenon Since it would be a bit too much to answer all your questions inside of those comments I've just set up an answer with a brief summary. – Aer0 Feb 07 '17 at 15:12
3 Answers
Starting a node script is pretty simple. Just use your command line or terminal and execute the following command.
node /path/to/your/file.js
Doing so you'll start your node script. If you're going to start a server it's pretty much the same. Just keep in mind to define a server in your node file and start it. A simple server using express could look like this (You can also use a fully node way, this is just a simple example using express. You may check google for how to set up a simple node http server).
var express = require('express');
var app = express();
var port = 4000;
app.listen(process.env.port || port);
As you can see the specified port is set to 4000. You can simply adjust this by changing the value itself or passing in a node environment variable. To pass in an environment variable just start your server like this.
node port=3000 /path/to/your/file.js
This will finally pass the value of port to process.env.port which obviously will start your server on the port 3000.
 
    
    - 3,792
- 17
- 33
- 
                    Ok. I got it up and running! Will need to do some more research on deploying it to an online web-hosting though in due time :-) – Aardo Feb 07 '17 at 15:22
you can use these packages to keep the file running so you won't have to login to server every time :-
forever and you can just write :-
forever start app.js
nodemon app.js
pm2 which is very useful , as it will auto restart your app when it crash or any error happens
pm2 start app.js
 
    
    - 2,802
- 2
- 14
- 33
- 
                    The question says that they script only does "Hello, world". Your answer is skipping the bit where you write the JavaScript to create an HTTP server. – Quentin Feb 07 '17 at 15:05
- 
                    check the comments blow his question , he asked about how he can keep the app running on server – Ahmed farag mostafa Feb 07 '17 at 15:06
Your run your file: node server.js
Then it starts.
There should be specified in code on which port you run your server. Then it is accessible for example at http://localhost:3000/
As Quentin noted, I was thinking about "creating web server". Of course, you can run javascript code with Node.js without server. Then skip the part abot localhost, just use node test.js in console.
 
    
    - 22,239
- 9
- 44
- 87
- 
                    1The question says that they script only does "Hello, world". Your answer is skipping the bit where you write the JavaScript to create an HTTP server. – Quentin Feb 07 '17 at 15:05
- 
                    
