Save it as server.js If you want to run this script all you have to do is go to the directory where the script is and run the following command:
node server.js
Another way to run a script is through the npm command, it requires a file named package.json at the root of the project, this way is more sophisticated because in the package.json file you can specify a command to run a script, so you can have several commands each one mapped to a script.
Also it helps to configure dependencies, versions and a lot of other stuff, you can find more about the package.json configuration in the following link:
https://www.npmjs.org/doc/json.html
Well for the purpose of the blog the file package.json will be very simple just to show how it works, so it will contain the following:
In the file the start command is mapped to the server.js script, once the file is saved in the directory where sript is located the following command will run the script:
npm start
Another way to run a script is with the help of packages one of them is nodemon (http://nodemon.io/), it stands for node monitor.
The description of nodemon says that it is a utility that will monitor for any changes in your source and automatically restart your server. Perfect for development. Using nodemon instead of node to run your code, and now your process will automatically restart when your code changes.
To install nodemon jus run the following command:
npm install -g nodemon
The -g means that it will install nodemon in a global way, you don't have to install it for every project.
Once installed you can see it working, just replace the node command for nodemon and run the script:
nodemon server.js
To see it working type the address http://127.0.0.1:1337/ in your browser and see the output, then modify the script to output instead "Hello world" to something else, reload the browser and see that the changes are reflect, so this is the big advantage of using nodemon you don't have to waste any time running the scripts while developing them.
Another package that does the same as nodemon is run (https://www.npmjs.org/package/run), install it just like nodemon:
npm install -g run
Then run the script with the run command
run server.js
Do the same exercise go the address http://127.0.0.1:1337/ see the output and modify the script, reload the page and see the changes.
So far in my way to node these are the different ways to run scripts, I hope you can learn something new from it.
Another way to run a script is through the npm command, it requires a file named package.json at the root of the project, this way is more sophisticated because in the package.json file you can specify a command to run a script, so you can have several commands each one mapped to a script.
Also it helps to configure dependencies, versions and a lot of other stuff, you can find more about the package.json configuration in the following link:
https://www.npmjs.org/doc/json.html
Well for the purpose of the blog the file package.json will be very simple just to show how it works, so it will contain the following:
In the file the start command is mapped to the server.js script, once the file is saved in the directory where sript is located the following command will run the script:
npm start
Another way to run a script is with the help of packages one of them is nodemon (http://nodemon.io/), it stands for node monitor.
The description of nodemon says that it is a utility that will monitor for any changes in your source and automatically restart your server. Perfect for development. Using nodemon instead of node to run your code, and now your process will automatically restart when your code changes.
To install nodemon jus run the following command:
npm install -g nodemon
The -g means that it will install nodemon in a global way, you don't have to install it for every project.
Once installed you can see it working, just replace the node command for nodemon and run the script:
nodemon server.js
To see it working type the address http://127.0.0.1:1337/ in your browser and see the output, then modify the script to output instead "Hello world" to something else, reload the browser and see that the changes are reflect, so this is the big advantage of using nodemon you don't have to waste any time running the scripts while developing them.
Another package that does the same as nodemon is run (https://www.npmjs.org/package/run), install it just like nodemon:
npm install -g run
Then run the script with the run command
run server.js
Do the same exercise go the address http://127.0.0.1:1337/ see the output and modify the script, reload the page and see the changes.
So far in my way to node these are the different ways to run scripts, I hope you can learn something new from it.
Is it possible to run the server.js from a remote server? something like this
ReplyDelete"scripts": {
"start": "node http://mydomain/server.js"
}
Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Node JS Online Training from India . or learn thru Javascript Online Training from India. Nowadays JavaScript has tons of job opportunities on various vertical industry. Javascript Online Training from India
ReplyDeleteHi, Great work.. The tutorial is just awesome. It is really helpful for a newbie like me. I am a regular follower of your blog. Really a very informative post you shared here. Kindly keep blogging. And if you are looking for VPS hosting provider? We provide you the best vps hosting germany service at a very cheap price.
ReplyDeleteThanks for Sharing a Very Informative Post & I read Your Article & I must say that is very helpful post for us.
ReplyDeleteDimension Control Services in Birmingham
plant engineering services in Hovedstaden Denmark
3D Laser Modelling
3d Laser Scanning Services in Glasgow
Point cloud to 3D Model Hovedstaden, Denmark
Scan to BIM in Georgia
Awesome post!
ReplyDeleteBest Electronics retail software