Linux installation Nodejs and with Nginx implementation of reverse proxy nodejs is what
node. JS is a JavaScript run environment (runtime). It actually encapsulates the Google V8 engine. The V8 engine executes JavaScript very fast and performs very well.
node. JS optimizes some of the special use cases, providing an alternative API that allows V8 to run better in a non-browser environment.
Local installation (OS X) version selection
- V4.4.4, long-term support version, mature and reliable
- V6.2.0 stable version, latest features
I'm still inclined to use the latest version here.
Download the installation package
https://nodejs.org/dist/v6.2.0/node-v6.2.0.pkg
Double-click Install installation package
Next step, the installation is complete.
Simple execution
node -v
v6.2.0
Local run (OS X) Create demo file
ConstHTTP =require(' http ');Consthostname =' 127.0.0.1 ';ConstPort = the;ConstServer = Http.createserver (req, res) = {Res.statuscode = $ ; Res.setheader (' Content-type ', ' Text/plain'); Res.end (' Hello world\n ');}); server. Listen (port, hostname, () = { console. log ('Server running at http://${hostname}:${port}/ `);});
Write to Fileexample.js
Execute file
node example.js
At this point the command line outputServer running at http://127.0.0.1:3000/
At the same time in the browser input http://127.0.0.1:3000/
, page outputHello World
Close the terminal and the page is no longer available.
Express Framework
We use the Express framework to build a website project demo.
npm install express
node_modules
Create a Demo.js file
varrequire(‘express‘);app = express(); app.use(express.static‘/public‘)); app.listen(8081)
Create folder in sibling folder public
, put static file inside1.jpg
In the browser inputhttp://127.0.0.1:8081/1.jpg
View Response Headers,X-Powered-By:Express
Server Installation (CentOS 7) Install node
curl--silent--locationhttps://rpm.nodesource.com/setup|bash-yum-yinstallnodejsyuminstallnpm
About the version of node
I last step through node installed version number is, at v0.10.42
first thought wrong, after looking at the data found that node has maintained a total of 4 versions
- v0.10.42 (LTS)
- v0.12.10 (LTS)
- 4.4.5 LTS
- 6.2
Oh da, really messy.
Writing Demo Instances
This part of the process is consistent with the above.
Install Forever and run
npm install forever -g
forever start app.js
Configure Nginx
cd /usr/local/nginx/conf/vhost/
vi demonode.coderfix.cn.conf
server80;server_name demonode.coderfix.cn; location / { proxy_pass http://127.0.0.1:8899; }}
Nginx parsing domain name, forwarded to the local Nodejs 8899 port ~
Configure domain name resolution and access
http://demonode.coderfix.cn/
This completes the deployment of Nodejs and Nginx.
Problems that may occur Nodejs service multiple open cause error
events.js: theThrow ER;//unhandled ' error ' event^Error:Listen Eaddrinuse at errnoexception (net. js:884: One) at Server._listen2 (net. js:1022: -) at Listen (net. js:1044:Ten) at Server. Listen(net. js:1110:5) at Object.<anonymous> (Foldername/app. js: -: -) at Module._compile (Module. js:456: -) at Object. Module. _extensions.. js(module. js:474:Ten) at Module. Load(module. js:356: +) at Function. Module. _load (module. js:312: A) at Function. Module. Runmain(module. js:497:Ten)
Turn off the process that was started before you open it.
grep nodekill -9****
Resources
- Http://nodejs.cn/doc/node/index.html
- http://nodejs.cn/download/
- Http://www.csdn.net/article/2013-08-28/2816731-absolute-beginners-guide-to-nodejs
- Https://cnodejs.org/topic/5021c2cff767cc9a51e684e3
- https://nodejs.org/en/blog/release/v0.10.42/
- Http://www.nodejs.net/a/20141030/122537.html
- Http://stackoverflow.com/questions/16827987/expressjs-throw-er-unhandled-error-event
"NodeJs" Linux installation NodeJs and with Nginx to implement reverse proxy