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

安装

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容