NodeJs 使用
1、安装NodeJS
Node.js 安装包及源码下载地址为:https://nodejs.org/en/download/。
下载安装后,打开控制台 cmd——输入node --version,如输出v0.00.00时,则安装成功
2、cmd cd /d E:\xxxx 为指定路径, cmd node为进入node命令
编写nodeserver.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/');
如何运行:
(1)初始化项目
执行cmd node安装目录 初始化项目 npm init (npm node package manager node包管理者)
cmd cd /d E:\node安装目录 node nodeserver.js
生成package.json
(2)运行nodejs.exe
(3)cmd——cd /d node安装目录
xxx>node nodeserver.js运行js代码
运行成功输出console.log内容,并在浏览器中输入127.0.0.1:8888,
如果网页中输出Hello World
报错解决:
node.js----microsoft jscript 运行时错误 800a138f 原因为:没有将项目初始化