Node.js是一个JavaScript平台,可以提供动态的响应式内容。JavaScript通常是一种客户端浏览器语言,如HTML或CSS。但是,Node.js是一个服务器端的JavaScript平台,可与PHP相媲美。Node.js通常可以与NGINX或Apache等其他流行的服务器应用程序一起使用。在本指南中,NGINX配置为处理前端静态文件请求,Node.js配置为处理后端文件请求。
终端登录新的Linode后,可以立即启动本指南,它是为root
用户编写的。但是,在安装之前,您可能希望确保Linode与我们的入门指南保持同步,并通过我们的“ 保护您的服务器”指南加以保护。
example.com
为您的域或IP地址:
/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;
}
}
default
符号链接:
rm defaultNGINX现已配置。但是,example.com
服务器块指向仍需要创建的目录和文件。
/var/www
和/var/www/example.com
目录:
mkdir -p /var/www/example.com<!DOCTYPE html>
<html>
<body>
<br>
<br>
<center>
<p>
<b>
If you have not finished the <a href="https://linode.com/docs/websites/nodejs/nodejs-nginx-debian">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 href="test.js">
<button type="button">Go to test.js</button>
</a>
</center>
</body>
</html>
NGINX现在正在侦听端口80并提供内容。它还配置为将/test.js
请求传递到端口3000.接下来的步骤是安装Node.js,然后使用Node.js编写服务器。新服务器侦听端口3000。
/var/www/example.com
目录中时,创建一个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
return
并运行Node.js服务器:
node server.jsCtrl+a
然后按下退出屏幕d
。NGINX正在侦听端口80并将任何/test.js
请求传递到端口3000.Node.js正在侦听端口3000并提供任何文件请求。接下来,写一个/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 type="button"
onclick="document.getElementById('sample').innerHTML = Date()">
Display the date and time.
</button>
<p id="sample"></p>
</center>
</body>
</html>
Node.js和NGINX正在合作。根据您的需要将请求路由到一台服务器或另一台服务器。Node.js提供了一个包含许多工具的大型API。使用Node.js,开发人员可以在客户端或服务器端工作时保持JavaScript语言。
对于后续步骤,请查看WebSockets,iframe或框架集等技术。要在JavaScript中进行开发,请尝试Express.js,Ember.js,jQuery或模块的节点包管理器。
有关此主题的其他信息,您可能需要参考以下资源。虽然提供这些是希望它们有用,但请注意,我们无法保证外部托管材料的准确性或及时性。
更多教程请前往腾讯云+社区学习更多知识。
参考文献:《https://www.linode.com/docs/development/nodejs/how-to-install-nodejs-and-nginx-on-debian/》