还是由于前段时间面试,面试官问我会不会node.js,下来后了解了一下node.js,并在centos上搭建来node.js的开发环境,做一小结
1.安装node.js运行环境
https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager
可参考上面地址,执行以下操作
Node.js and npm are available from the Fedora Extra Packages for Enterprise Linux (EPEL) repository. If you haven't already done so, first enable EPEL.
To check if you have EPEL, run
yum repolist
if you don't see epel, download it (At the time of this writing, the last version is 6.8.)
curl -O http://download-i2.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
then install it
sudo rpm -ivh epel-release-6-8.noarch.rpm
And then run the following command to install node and npm:
sudo yum install npm
安装完后在运行yum repolist
如果有epel就ok了
2.编写node.js脚本
脚本参考http://www.oschina.net/question/129540_21801
// these modules need to be imported in order to use them. // Node has several modules. They are like any #include // or import statement in other languages var http = require("http"); var url = require("url"); // The most important line in any Node file. This function // does the actual process of creating the server. Technically, // Node tells the underlying operating system that whenever a // connection is made, this particular callback function should be // executed. Since we're creating a web service with REST API, // we want an HTTP server, which requires the http variable // we created in the lines above. // Finally, you can see that the callback method receives a 'request' // and 'response' object automatically. This should be familiar // to any PHP or Java programmer. http.createServer(function(request, response) { // The response needs to handle all the headers, and the return codes // These types of things are handled automatically in server programs // like Apache and Tomcat, but Node requires everything to be done yourself response.writeHead(200, {"Content-Type": "text/plain"}); // Here is some unique-looking code. This is how Node retrieves // parameters passed in from client requests. The url module // handles all these functions. The parse function // deconstructs the URL, and places the query key-values in the // query object. We can find the value for the "number" key // by referencing it directly - the beauty of JavaScript. var params = url.parse(request.url, true).query; var input = params.number; // These are the generic JavaScript methods that will create // our random number that gets passed back to the caller var numInput = new Number(input); var numOutput = new Number(Math.random() * numInput).toFixed(0); // Write the random number to response response.write(numOutput); // Node requires us to explicitly end this connection. This is because // Node allows you to keep a connection open and pass data back and forth, // though that advanced topic isn't discussed in this article. response.end(); // When we create the server, we have to explicitly connect the HTTP server to // a port. Standard HTTP port is 80, so we'll connect it to that one. }).listen(80); // Output a String to the console once the server starts up, letting us know everything // starts up correctly console.log("Random Number Generator Running...");
3.运行node.js服务器
运行sudo node mynode.js,注意如果不以su运行会报Error: EACCES,Permission denied
错误。ok,开始服务端监听,客服端可以访问这个restful服务了
http://localhost/?number=27
多次刷新会有不同的随机数出现