Linux下nodejs(一):安装和使用

来源:互联网 发布:eve数据地点出什么 编辑:程序博客网 时间:2024/06/16 13:34

一、安装

一共有三种安装方式,由于其他两种不太方便,因此只介绍第一种二进制文件安装。

1、首先官网下载nodejs安装压缩包。

这里写图片描述

2、下载完成后用ftp上传到Linux任意文件夹即可。我的是/home/chuan/chuansoft。然后进入到此文件夹。

cd /home/chuan/chuansoft

3、在此目录下解压

#tar xf node-v8.3.0-linux-x64.tar.xz

4、解压后进入到解压后的文件夹。

#cd node-v8.3.0-linux-x64

5、设置node和npm为全局变量。

#ln -s /home/chuan/chuansoft/node-v8.3.0-linux-x64/bin/node /usr/local/bin/node#ln -s /home/chuan/chuansoft/node-v8.3.0-linux-x64/bin/npm /usr/local/bin/npm

6、随便进入到其他文件夹测试。

#node -v#npm -v

参考文章

http://www.cnblogs.com/8765h/p/4777746.html

http://www.cnblogs.com/dubaokun/p/3558848.html

http://www.xitongzhijia.net/xtjc/20150202/36680.html

二、使用

1、新建hello.js文件。

#vi hello.js

2、键入以下代码。

var http = require("http");http.createServer(function(request, response) {    response.writeHead(200, {        "Content-Type" : "text/plain" // 输出类型    });    response.write("this is before;Hello World");// 页面输出    response.end();}).listen(8100); // 监听端口号console.log("this is after;nodejs start listen 8100 port!");

3、node运行hello.js文件。

#node hello.js

this is after;nodejs start listen port!

4、浏览器输入http://12.34.567.89:8100(主机IP:8100端口)

this is before ;Hello World

至此完成!
参考文章http://www.zuidaima.com/share/2621948436794368.htm

原创粉丝点击