安装包下载地址
https://nodejs.org/en/download/
支持Windows、Linux、macOS

环境配置(Windows)
检测是否配置了Node.js的环境变量
开始=》运行=》输入"cmd" => 输入命令"path"
PATH=C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files\TortoiseSVN\bin;C:\Program Files\TortoiseGit\bin;D:\软件\Git\cmd;C:\Program Files\nodejs\;C:\Users\86177\AppData\Local\Programs\Python\Python38\Scripts\;C:\Users\86177\AppData\Local\Programs\Python\Python38\;C:\Users\86177\AppData\Local\Microsoft\WindowsApps;;D:\软件\Microsoft VS Code\bin;D:\软件\PyCharm 2019.3\bin;;D:\软件\IntelliJ IDEA 2019.3\bin;;C:\Users\86177\AppData\Roaming\npm
检查Node.js版本
node --version
创建第一个应用程序
使用VSCode,创建“demo1.js”文件
使用 require 指令来载入 http 模块,并将实例化的 HTTP 赋值给变量 http,实例如下:
var http = require("http");
使用 http.createServer() 方法创建服务器,并使用 listen 方法绑定 8888 端口。 函数通过 request, response 参数来接收和响应数据。
实例如下,在你项目的根目录下创建一个叫 server.js 的文件,并写入以下代码:
var http = require('http');http.createServer(function (request, response) {
// 发送 HTTP 头部 // HTTP 状态值: 200 : OK // 内容类型: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// 发送响应数据 "Hello World"
response.end('Hello World\n');}).listen(8888);// 终端打印如下信息
console.log('Server running at http://127.0.0.1:8888/');
使用 node 命令执行以上的代码:
node demo1.js
Server running at http://127.0.0.1:8888/


网友评论