Node.js是一个开源的、跨平台的JavaScript运行环境,用于在服务器端运行JavaScript代码。它提供了一个简单的API,可以用于开发各种网络和服务器应用程序。
以下是Node.js的安装和使用的详细步骤和代码示例:
访问Node.js官方网站,下载适合你操作系统的版本。对于Windows用户,可以下载.msi文件,对于Mac用户,可以下载.pkg文件,对于Linux用户,可以下载.tar.gz文件。
官网首页:推荐18.17.1 LTS
版本
对于Windows和Mac用户,可以双击下载的安装文件,按照提示安装。对于Linux用户,可以解压下载的.tar.gz文件,进入解压后的目录,执行以下命令安装:
sudo apt-get update
sudo apt-get install nodejs
下面介绍Windows版安装过程:一直点击next,就安装完成。
安装完成后,打开终端或命令行窗口,输入以下命令验证Node.js是否成功安装:
node -v
输出:v18.17.1
如果输出了Node.js的版本号,说明安装成功。
创建一个名为app.js的文件,输入以下代码:
const os = require('node:os');
let cups = os.cpus();
console.log(cups);
运行命令:node .\demo.js
这是一个简单的Node.js程序,运行输出:
[
{
model: 'Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz',
speed: 2904,
times: {
user: 6470625,
nice: 0,
sys: 10327390,
idle: 1041758375,
irq: 626765
}
},
{
model: 'Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz',
speed: 2904,
times: {
user: 6784390,
nice: 0,
sys: 8064656,
idle: 1043707046,
irq: 85984
}
},
{
model: 'Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz',
speed: 2904,
times: {
user: 8906109,
nice: 0,
sys: 12502437,
idle: 1037147546,
irq: 183734
}
},
{
model: 'Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz',
speed: 2904,
times: {
user: 5545609,
nice: 0,
sys: 10756093,
idle: 1042254390,
irq: 75453
}
},
{
model: 'Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz',
speed: 2904,
times: {
user: 5543031,
nice: 0,
sys: 7715734,
idle: 1045297312,
irq: 95156
}
},
{
model: 'Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz',
speed: 2904,
times: {
user: 4580484,
nice: 0,
sys: 9040343,
idle: 1044935250,
irq: 63671
}
},
{
model: 'Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz',
speed: 2904,
times: {
user: 8635468,
nice: 0,
sys: 16050562,
idle: 1033870046,
irq: 121609
}
},
{
model: 'Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz',
speed: 2904,
times: {
user: 6555671,
nice: 0,
sys: 15675906,
idle: 1036324500,
irq: 44578
}
},
{
model: 'Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz',
speed: 2904,
times: {
user: 5325343,
nice: 0,
sys: 8493781,
idle: 1044736953,
irq: 103296
}
},
{
model: 'Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz',
speed: 2904,
times: {
user: 4272343,
nice: 0,
sys: 8286390,
idle: 1045997343,
irq: 69843
}
},
{
model: 'Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz',
speed: 2904,
times: {
user: 7835000,
nice: 0,
sys: 13760187,
idle: 1036960890,
irq: 118390
}
},
{
model: 'Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz',
speed: 2904,
times: {
user: 6787234,
nice: 0,
sys: 15863359,
idle: 1035905484,
irq: 56687
}
},
{
model: 'Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz',
speed: 2904,
times: {
user: 5320062,
nice: 0,
sys: 8372703,
idle: 1044863296,
irq: 202765
}
},
{
model: 'Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz',
speed: 2904,
times: {
user: 4378234,
nice: 0,
sys: 8924750,
idle: 1045253078,
irq: 35968
}
},
{
model: 'Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz',
speed: 2904,
times: {
user: 7873718,
nice: 0,
sys: 11812968,
idle: 1038869375,
irq: 104671
}
},
{
model: 'Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz',
speed: 2904,
times: {
user: 8687765,
nice: 0,
sys: 14733875,
idle: 1035134421,
irq: 100234
}
}
]
创建一个Node.js项目需要执行以下步骤:
npm init -y
该命令将创建一个名为package.json的文件,其中包含有关项目的元数据和其他信息。
{
"name": "nodedemo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
npm install express
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
node index.js