nodejs学习资料 - 第一节:安装并运行helloword!

2016-11-29  本文已影响49人  会煮咖啡的猫咪

安装

https://nodejs.org/en/download/
建议用安装包

yum install -y gcc-c++ make
curl -sL https://rpm.nodesource.com/setup_6.x | sudo -E bash -

yum -y install nodejs
yum -y install nodejs npm
yum remove nodejs npm -y

- 清理目录
/usr/local/lib
/usr/local/include
$ sudo npm install npm -g

测试环境 helloword

console.log("helloword!");
$ node helloword.js
helloword

创建一个 web server

var http = require("http")
http.createServer(function(request, response){
  response.writeHead(200,{'content-Type':'text/plain'});
  response.end('hello word!');
}).listen(8888);

console.log('web server is running! http://127.0.0.1:8888/');

require 调用 http 模块
服务listen监听在 8888 端口

$ node webserver.js

浏览器输入 http://127.0.0.1:8888/

参考

https://nodejs.org/en/docs/guides/anatomy-of-an-http-transaction/

代码

https://github.com/hans007/JavaScriptCodes/tree/master/nodejs-do

上一篇 下一篇

猜你喜欢

热点阅读