美文网首页
nodejs学习资料 - 第一节:安装并运行helloword!

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

作者: 会煮咖啡的猫咪 | 来源:发表于2016-11-29 11:21 被阅读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

  • 新建文件 helloword.js
console.log("helloword!");
  • 运行
$ node helloword.js
  • 输出
helloword

创建一个 web server

  • 新建文件 webserver.js
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

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/nvpapttx.html