Nodejs环境搭建

一. 安装NodeJs

1. 到Node中文网下载安装nodean安装包

$ VERSION=v12.9.1
$ wget https://npm.taobao.org/mirrors/node/${VERSION}/node-${VERSION}-linux-x64.tar.xz
$ tar -xvf node-${VERSION}-linux-x64.tar.xz -C /usr/local/                 #解压
$ cd /usr/local/ && mv ./node-${VERSION}-linux-x64/ ./node                 #重命名

2. 配置环境变量

$ vim /etc/profile               
#******************内容******************
export PATH=/usr/local/node/bin:$PATH
#*******************************************

$ source /etc/profile            #刷新环境变量
$ node -v                        #测试
$ npm -v

3. 命令行交互模式

[root@shs ~]# node
Welcome to Node.js v12.9.1.
Type ".help" for more information.
> console.log('Hello World');
Hello World

二. Web示例

1. 创建WebServer文件

$ vim ./hello_server.js
var http = require('http');
http.createServer(function (request, response) {
    response.writeHead(200, { 'Content-Type': 'text/plain' });
    response.end('Hello World\n');
}).listen(3000);

console.log('Server listen :3000/');

2. 启动运行Server

$ node ./hello_server.js &
# $ ps -ef | grep hello_server 
# $ kill -9 XXXID    #杀死进程

3. 查看结果

curl http://0.0.0.0:3000

三. 安装TypeScript

1. 使用Npm安装ts: 可参考 TypeScript中文网

$ npm install -g typescript     #安装
$ npm update  -g typescript     #更新
$ tsc -v                        #检查ts版本

2. 简单的ts示例

$ vim ./hello.ts    #创建一个ts文件
class HelloWorld {
    Say(content:string) {
        console.log(content);
    }
}

var hw=new HelloWorld();
hw.Say("Hello TypeScript");
$ tsc hello.ts     #编译
$ node ./hello.js  #运行

在这里插入图片描述

参考学习:
http://nqdeng.github.io/7-days-nodejs/#4.2.1

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值