在Windows11上安装Node.js,执行JavaScript,构建React环境
试着在Windows11上安装Node.js并运行JavaScript,搭建React环境。
(Note: The paraphrase is provided in simplified Chinese. If you require the text in traditional Chinese, kindly let me know.)
-
- Node.jsインストール
-
- Node.js で JavaScript実行(簡単なHTTPサーバたてる)
- React環境構築
Node.js 安装(node:v18.15.0, npm:v9.5.0)
-
- 获取Node.js安装程序(选择LTS版本)
-
- https://nodejs.org/
执行安装程序
按照屏幕提示继续,确认许可并选择“是”
中途会出现命令提示符或者PowerShell,按照屏幕提示进行键盘输入
即使出现警告也不必担心,可以继续安装
确认安装
在命令提示符或者PowerShell中输入以下内容来确认版本
# Node
node –version
# Node Package Manager
npm –version
在Node.js中执行JavaScript(HTTP服务器)。
-
- 创建一个合适的文件夹
-
- C:\nodework
在文件夹中创建JavaScript index.js
C:\nodework\index.js
在index.js中输入以下代码
index.js
// 模块导入
const http = require(“http”);
// HTTP服务器设置
http.createServer((request, response) => {
switch (request.url) {
case ‘/json’:
response.writeHead(200, { “content-type”: “application/json”, “charset”: “utf-8” });
response.write(“{ text : Hello World }”);
response.end();
break;
case ‘/html’:
response.writeHead(200, { “content-type”: “text/html”, “charset”: “utf-8” });
response.write(“<!doctype html>
Hello World
“);
response.end();
break;
default:
response.writeHead(200, { “content-type”: “text/plain” });
response.write(“Hello World”);
response.end();
}
}).listen(8888);
在命令提示符或PowerShell中移动到所创建的文件夹
cd C:\nodework
在命令提示符或PowerShell中使用Node执行JavaScript
通过以下命令执行JavaScript来自Node
node index.js
# 当执行时出现Windows安全警告时,可以放心点击取消
# 如果想要从其他计算机或设备进行连接,请允许访问权限
访问已执行的内容
如果在浏览器中访问以下内容,将显示Hello World
http://localhost:8888/
http://localhost:8888/json
http://localhost:8888/html
構建React環境(create-react-app:v5.0.1)。
参考公式的 Create a New React App,试着做一做
