Nodejs 与npm

Web 服务器开发: Node.js 可以用于构建高性能的 Web 服务器,处理大量的并发请求。许多知名的网站和应用都使用 Node.js 作为服务器端技术,如 Netflix、PayPal 等。 实时应用开发: 由于 Node.js 的非阻塞 I/O 和事件驱动特性,非常适合开发实时应用,如在线聊天应用、实时游戏、 实时数据监控系统等。 命令行工具开发: 开发者可以使用 Node.js 编写命令行工具,利用其丰富的模块和跨平台特性,方便地实现各种功能,如 文件处理、代码编译、自动化部署等。 微服务架构: Node.js 可以用于构建微服务架构中的各个服务,通过 npm 模块和 HTTP 协议进行服务之间的通信和 协作,实现系统的高可扩展性和灵活性。

87 mkdir -p /data/codes/npm-project

88 cd /data/codes/npm-project

89 npm init

90 apt install nodejs npm

91 node -v
cat > server.js << eof

const http = require('http');

// 创建一个 HTTP 服务器

const server = http.createServer((req, res) => {

// 设置响应头

res.writeHead(200, { 'Content-Type': 'text/plain' });

// 发送响应内容

res.end('Hello, World!\n');

});

// 监听 3000 端口

server.listen(3000, '0.0.0.0', () => {

console.log('Server running at http://0.0.0.0:3000/');

});

eof
node server.js

curl 10.0.0.11:3000

Hello, World!

npm

npm(Node Package Manager)是 Node.js 的默认包管理器,类似于手机应用市场。其主要功能是 管理 Node.js 项目中的各种依赖包。npm 拥有巨大的软件仓库,汇聚了海量 Node.js 包,功能覆盖网络 编程、数据库交互、前端框架搭建等众多领域, 比如网络编程、文件操作、数据库管理、前端框架等,开发者 可以在其中找到满足自己项目需求的各种包。

mkdir /data/codes/npm-project

cd /data/codes/npm-project

npm init

npm init

This utility will walk you through creating a package.json file.

It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields

and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and

save it as a dependency in the package.json file.

Press ^C at any time to quit.

package name: (npm-project) #直接Enter

version: (1.0.0) #直接Enter

description: 测试项目 #简单描述

entry point: (index.js) #直接Enter

test command: #直接Enter

git repository: #直接Enter

keywords: #直接Enter

author: zz_zjx 作者

license: (ISC) #直接Enter

Is this OK? (yes) # 直接Enter
为了避免获取互联网的软件包过于缓慢,定制专属的镜像源

npm config set registry https://registry.npmmirror.com

在当前目录下生产一个隐藏的配置文件

cat .npmrc

registry=https://registry.npmmirror.com/

安装 express 框架作为项目依赖,express 是一个流行的 Node.js Web 应用框架。

npm install express --registry=https://registry.npmmirror.com

express 安装到项目的 node_modules 目录下,并在 package.json 文件中添加相应的依赖项。

ls

node_modules package.json package-lock.json

root@ubuntu24:npm-project# tree -L 1 node_modules node_modules ├── accepts ├── array-flatten ... ├── utils-merge └── vary 68 directories, 0 files

查看包依赖

head package-lock.json

{

"name": "npm-project",

"version": "1.0.0",

"lockfileVersion": 3,

"requires": true,

"packages": {

"": {

"name": "npm-project",

"version": "1.0.0",

"license": "ISC",
创建入口文件 index.js

cat > index.js << eof

const express = require('express');

const app = express();

const port = 3000;

// 定义简单的路由

app.get('/', (req, res) => {

res.send('站点首页!!!\n');

});

app.get('/login', (req, res) => {

res.send('站点登录页面!!!\n');

});

app.get('/register', (req, res) => {

res.send('站点注册页面!!!\n');

});

// 启动服务器

app.listen(port, () => {

console.log(`Server is running on port ${port}`);

});

eof

为了方便启动项目,你可以在 package.json 文件中添加一个启动脚本

vim package.json

{

"name": "npm-project",

"version": "1.0.0",

"description": "测试项目",

"main": "index.js",

"scripts": {

"start": "node index.js" #修改此项

},

"author": "zz_zjx",

"license": "ISC",

"dependencies": {

"express": "^5.1.0"

}

}

运行

npm start

curl 10.0.0.11:3000

站点首页!!!

root@ubuntu11:~# curl 10.0.0.11:3000/login

站点登录页面!!!

root@ubuntu11:~# curl 10.0.0.11:3000/register

相关推荐
H5css�海秀2 小时前
今天是自学大模型的第一天(sanjose)
后端·python·node.js·php
Z兽兽2 小时前
React@18+Vite项目配置env文件
前端·react.js·前端框架
SuniaWang2 小时前
《Spring AI + 大模型全栈实战》学习手册系列 · 专题六:《Vue3 前端开发实战:打造企业级 RAG 问答界面》
java·前端·人工智能·spring boot·后端·spring·架构
A_nanda3 小时前
根据AI提示排查vue前端项目
前端·javascript·vue.js
happymaker06263 小时前
web前端学习日记——DAY05(定位、浮动、视频音频播放)
前端·学习·音视频
~无忧花开~4 小时前
React状态管理完全指南
开发语言·前端·javascript·react.js·前端框架
LegendNoTitle4 小时前
计算机三级等级考试 网络技术 选择题考点详细梳理
服务器·前端·经验分享·笔记·php
@大迁世界4 小时前
1.什么是 ReactJS?
前端·javascript·react.js·前端框架·ecmascript
BJ-Giser5 小时前
Cesium 基于EZ-Tree的植被效果
前端·可视化·cesium
王码码20356 小时前
Flutter for OpenHarmony:Flutter 三方库 algoliasearch 毫秒级云端搜索体验(云原生搜索引擎)
android·前端·git·flutter·搜索引擎·云原生·harmonyos