在Linux系统中搭建Node.js开发环境的简单步骤讲解
网络编程 2021-07-04 21:04www.168986.cn编程入门
这篇文章主要介绍了在Linux系统中搭建Node.js开发环境的步骤,Node使得JavaScript程序可以在本地操作系统环境中解释运行,需要的朋友可以参考下
1. Linux安装node.js
ubuntu:
sudo apt-get install nodejs npm
centos:
yum install nodejs npm
更详细的安装参见:https://github./joyent/node/wiki/Installation
npm为类似PHP中Pear的包管理器
2. 开始使用node.js
用文本编辑器新建hello.js写入以下内容
console.log('hello world');
打开命令行输入
$ node hello.js
你会看到输出
$ hello world
console.log是最常用的输出指令
3. 建立HTTP服务器
理解node.js架构
像PHP的架构模型为:
浏览器--》HTTP服务器(apache、nginx)--》PHP解释器
而在node.js应用中,node.js采用:
浏览器--》node.js这种架构
创建HTTP服务器:新建一个app.js文件,内容如下:
var http = require('http'); http.createServer(function(req, res){ res.writeHead(200,{'Content-Type': 'text/html'}); res.write('</pre> <h1>node.js</h1> <pre>'); res.end(' hello world '); }).listen(3000); console.log("http server is listening at port 3000.");
运行
$ node app.js
打开浏览器打开http://127.0.0.1:3000查看结果
该程序调用了node.js提供的http模块,对所有的Http请求答复同样的内容并监听3000端口。运行这个脚本后不会立刻退出,必须按下ctro+c才会停止,这是因为listen函数创建了事件监听器。
4. 调试脚本
node.js脚本修改后,必须停止原程序,重新运行,才能看到变化。
用包管理器安装supervisor工具。
$ npm install -g supervisor
以后通过
$ supervisor app.js
來运行node.js程序,它会检测程序代码变化,自动重启程序。
注意:安装时需要获得root权限。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程