Nodejs环境搭建


文章目录

  • 一. 安装NodeJs
  • 二. Web示例
  • 三. 安装TypeScript

一. 安装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示例 【Nodejs环境搭建】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#运行

Nodejs环境搭建
文章图片

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

    推荐阅读