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

相关推荐
roamingcode2 小时前
Claude Code NPM 包发布命令
前端·npm·node.js·claude·自定义指令·claude code
码哥DFS2 小时前
NPM模块化总结
前端·javascript
gc_22992 小时前
运行npm run命令报错“error:0308010C:digital envelope routines::unsupported”
npm·node.js
灵感__idea2 小时前
JavaScript高级程序设计(第5版):代码整洁之道
前端·javascript·程序员
唐璜Taro2 小时前
electron进程间通信-IPC通信注册机制
前端·javascript·electron
陪我一起学编程4 小时前
创建Vue项目的不同方式及项目规范化配置
前端·javascript·vue.js·git·elementui·axios·企业规范
LinXunFeng4 小时前
Flutter - 详情页初始锚点与优化
前端·flutter·开源
GISer_Jing4 小时前
Vue Teleport 原理解析与React Portal、 Fragment 组件
前端·vue.js·react.js
Summer不秃5 小时前
uniapp 手写签名组件开发全攻略
前端·javascript·vue.js·微信小程序·小程序·html
coderklaus5 小时前
Base64编码详解
前端·javascript