Simple steps to build the Node. js development environment in Linux, linuxnode. js
1. Install node. js in Linux
Ubuntu:
sudo apt-get install nodejs npm
Centos:
yum install nodejs npm
For more detailed installation see: https://github.com/joyent/node/wiki/Installation
Npm is a package manager similar to Pear in PHP
2. Start using node. js
Use the text editor to create hello. js and write the following content
console.log('hello world');
Open command line Input
$ node hello.js
You will see the output
$ hello world
Console. log is the most common output command.
3. Create an HTTP server
Understanding node. js Architecture
The PHP architecture model is:
Browser -- HTTP server (apache, nginx) -- PHP interpreter
In node. js applications, node. js adopts the following methods:
Browser -- node. js Architecture
Create an HTTP server: Create an app. js file with the following content:
var http = require('http');http.createServer(function(req, res){ res.writeHead(200,{'Content-Type': 'text/html'}); res.write('</pre>
Run
$ node app.js
Open the browser and open http: // 127.0.0.1: 3000 to view the result.
This program calls the http module provided by node. js, replies the same content to all Http requests, and listens to port 3000. After the script is run, it does not exit immediately. You must press ctro + c to stop it. This is because the listen function creates an event listener.
4. debug the script
After the node. js script is modified, you must stop the original program and run it again to see the changes.
Use the Package Manager to install the supervisor tool.
$ npm install -g supervisor
Later pass
$ supervisor app.js
To run the node. js program. It will detect program code changes and automatically restart the program.
Note: You must obtain the root permission during installation.
Articles you may be interested in:
- Use the Package Manager in linux to install node. js
- Node. js getting started Tutorial: install and configure Node. js on windows and Linux
- Enable ECMAScript 6 in NodeJS (windos and Linux)
- Use forever in Linux to implement self-starting Node. js Projects
- Start nodemanager on linux