Node.js是一个JavaScript平台,可以动态,快速正确响应请求。 JavaScript通常是客户端,浏览器语言如HTML或CSS。 但是,Node.js是一个服务器端的JavaScript平台,与PHP相当。 Node.js经常与其他流行的服务器应用程序(如NGINX或Apache)一起工作。 在本文中,NGINX配置为处理前端,静态文件请求,Node.js配置为处理后端文件请求。
安装配置NGINX
1.安装
2.启动Nginx
3.切换目录到sites-available
- cd /etc/nginx/sites-available/
4.新建一个sites-available文件,把example.com替换成你的域名
/etc/nginx/sites-available/example.com:
- #Names a server and declares the listening port
- server {
- listen 80;
- server_name example.com www.example.com;
-
- #Configures the publicly served root directory
- #Configures the index file to be served
- root /var/www/example.com;
- index index.html index.htm;
-
- #These lines create a bypass for certain pathnames
- #www.example.com/test.js is now routed to port 3000
- #instead of port 80
- location /test.js {
- proxy_pass http://localhost:3000;
- proxy_set_header Host $host;
- }
- }
5.切换目录到sites-enabled
- cd /etc/nginx/sites-enabled/
6.创建一个软链接指向example sites-available文件
- ln -s /etc/nginx/sites-available/example.com
7.删除default软链接
8.重载nginx配置
创建目录和HTML Index文件
NGINX现已配置。 但是,example.com server块里涉及的目录和文件需要手动创建。
1.创建/var/www和/var/www/example.com目录
- mkdir -p /var/www/example.com
2.切换目录
3.创建HTML Index文件
/var/www/example.com/index.html:
- <!DOCTYPE html>
- <html>
- <body>
-
- <br>
- <br>
-
- <center>
- <p>
- <b>
- If you have not finished the <a>guide</a>, the button below will not work.
- </b>
- </p>
- </center>
-
- <center>
- <p>
- The button links to test.js. The test.js request is passed through NGINX and then handled by the Node.js server.
- </p>
- </center>
-
- <center>
- <a>
- <button>Go to test.js</button>
- </a>
- </center>
-
- </body>
- </html>
安装Node.js
NGINX现在正在侦听80端口并已能提供服务。 它还配置/test.js请求转到端口3000.接下来的步骤是安装Node.js,然后使用Node.js编写服务器。 新服务器侦听端口3000。
1.安装Node版本管理器
- curl https://raw.githubusercontent.com/creationix/nvm/v0.20.0/install.sh | bash
2.关闭并重新打开终端
3.安装Node.js
4.创建一个Node.js服务器
/var/www/example.com/server.js:
- //nodejs.org/api for API docs
- //Node.js web server
- var http = require("http"), //Import Node.js modules
- url = require("url"),
- path = require("path"),
- fs = require("fs");
-
- http.createServer(function(request, response) { //Create server
- var name = url.parse(request.url).pathname; //Parse URL
- var filename = path.join(process.cwd(), name); //Create filename
- fs.readFile(filename, "binary", function(err, file) { //Read file
- if(err) { //Tracking Errors
- response.writeHead(500, {"Content-Type": "text/plain"});
- response.write(err + "\n");
- response.end();
- return;
- }
- response.writeHead(200); //Header request response
- response.write(file, "binary"); //Sends body response
- response.end(); //Signals to server that
- }); //header and body sent
- }).listen(3000); //Listening port
- console.log("Server is listening on port 3000.") //Terminal output
5.运行一个新的screen会话
6.回车运行Node.js服务器
7.Ctrl+a+d退出screen
创建Test.js文件
NGINX目前侦听端口80,并将任何/test.js请求传递到端口3000。Node.js侦听端口3000并对任何请求响应。 接下来,写一个/test.js文件。
1.创建文件
/var/www/example.com/test.js:
- <!DOCTYPE html>
- <html>
- <body>
-
- <center>
- <h2>
- Your Node.JS server is working.
- </h2>
- </center>
-
- <center>
- <p>
- The below button is technically dynamic. You are now using Javascript on both the client-side and the server-side.
- </p>
- </center>
- <br>
-
- <center>
- <button
- sample').innerHTML = Date()">
- Display the date and time.
- </button>
- <p></p>
- </center>
-
- </body>
- </html>
2.浏览器输入IP地址或域名测试NGINX服务器。 点击“Go to test.js”按钮测试Node.js服务器是否正常。 在测试页上,“Display the date and time”按钮将执行javascript的客户端代码段,以返回当前时间。
Node.js和NGINX目前已经能一起正常运行。 根据您的需要你可以更新路由把请求转到任何服务器。 Node.js提供了许多API及工具。 使用Node.js,开发人员可以在客户端或服务器端工作时使用JavaScript语言。