惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
T
The Blog of Author Tim Ferriss
P
Proofpoint News Feed
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
小众软件
小众软件
美团技术团队
Martin Fowler
Martin Fowler
爱范儿
爱范儿
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Security Blog
Microsoft Security Blog
宝玉的分享
宝玉的分享
J
Java Code Geeks
B
Blog
V
V2EX
Stack Overflow Blog
Stack Overflow Blog
B
Blog RSS Feed
博客园 - Franky

姓王者的博客

Linux用户Secure Boot自主维护指南 | 姓王者的博客 MAD Bugs 已经开始——关于信息安全的军备竞赛 | 姓王者的博客 解决钉钉Dingtalk无法在Linux新版内核上启动问题-修复可执行栈错误 | 姓王者的博客 突发:GitHub 正遭受大规模 Issue 赌博广告轰炸 | 姓王者的博客 Ubuntu26.04-beta体验:坚毅浣熊! | 姓王者的博客 fakeclaw装作龙虾发贴吧 | 姓王者的博客 找回12年前的QQ记忆 | 姓王者的博客 在Linux上玩Flash网页游戏-洛克王国 | 姓王者的博客 Copilot将使用交互数据来训练 | 姓王者的博客 重要通知-请更新我的GPG公钥 | 姓王者的博客 为了自由Android | 姓王者的博客 GPL"2,3"事 | 姓王者的博客 短文-对VitePlus的一点🤏小贡献 | 姓王者的博客 Bing收录没了?亲测有效的快速恢复指南 | 姓王者的博客 解决桌面设备二维码快速识别的工具-ClipQR | 姓王者的博客 解决 Nautilus 自定义终端插件安装依赖问题 | 姓王者的博客 OpenClaw 该熄火了 | 姓王者的博客 Vite8 - 统一的基建开始 | 姓王者的博客 Astro 6 推出啦 | 姓王者的博客 ubuntu的openvpn异常暂停推送更新 | 姓王者的博客 Ubuntu 24.04 安装 Win10 虚拟机 | 姓王者的博客 ESA-后记:热爱阿里云 | 姓王者的博客 Moonbit 0.8.0 重大发布,我也要改一下我的包 | 姓王者的博客 ESA Pages 边缘开发大赛获奖 | 姓王者的博客 Astro: 优化katex,mermaid和灯箱使用 | 姓王者的博客 从edgeone迁移到esa | 姓王者的博客 出租人类:AI时代的荒诞与真实 | 姓王者的博客 Astro 5.17构建性能优化实践:从18s到13s | 姓王者的博客 Moonbit License Checker 开发使用 | 姓王者的博客 Stalux Astro博客主题自荐 | 姓王者的博客
node.js:试试express | 姓王者的博客
作者:xingwangzhe · 2024-12-18 · via 姓王者的博客

🕒 阅读时间:1 分钟 📝 字数:272 👀 阅读量: Loading...

Express 框架是一个快速、开放、极简的 web 应用开发框架,用于构建灵活和强大的 web 应用和 API。它是 Node.js 平台上最流行的框架之一,因为它提供了一套丰富的功能来简化和加速 web 开发过程。

使用

引入头

const express = require("express");

const app = express();

样例

id

const express = require("express");

const singers = require("./singers.json").singers;

const app = express();

console.log(singers);

app.get("/singers/:id", (req, res) => {

let id = req.params.id;

let singer = singers.find((singer) => {

if (singer.id == id) {

res.send(singer.name);

}

});

});

app.listen(3000, () => {

console.log("Server is running on port 3000");

});

验证

const express = require("express");

const fs = require("fs");

const app = express();

const path = require("path");

function recordMiddleware(req, res, next) {

let { url, ip } = req;

console.log(`用户访问了${url},IP地址为${ip}`);

fs.appendFile(path.resolve(__dirname, "./access.log"), `${url} ${ip}\n`, (err) => {

if (err) {

console.log(err);

}

});

next();

}

function checkcoode(req, res, next) {

let { code } = req.query;

if (code === "666") next();

else res.send("验证码错误");

}

app.use(express.static(path.resolve(__dirname, "./")));

app.use(recordMiddleware);

//前台首页

app.get("/home", checkcoode, (req, res) => {

res.send("首页你好");

});

app.get("/admin", checkcoode, (req, res) => {

res.send("后台管理你好");

});

app.get("*", (req, res) => {

res.send("404 Not Found");

});

app.listen(3000, () => {

console.log("服务器启动成功,端口号为3000");

});

//后台首页

防盗链

const express = require("express");

const app = express();

const path = require("path");

app.use((req, res, next) => {

let reffer = req.get("Referrer");

if (reffer) {

let url = new URL(reffer);

let hostname = url.hostname;

console.log(hostname);

if (hostname !== "127.0.0.1") {

console.log("not 127");

res.status = 404;

res.end("Not Found");

}

}

next();

});

app.use(express.static(path.resolve(__dirname, "./")));

app.listen(3000, () => {

console.log("Server is running on port 3000");

});