当前位置:实例文章 » HTML/CSS实例» [文章]【Nodejs】原生nodejs路由、获取参数、静态目录

【Nodejs】原生nodejs路由、获取参数、静态目录

发布人:shili8 发布时间:2025-02-28 00:01 阅读次数:0

**原生 Node.js 路由**

在 Node.js 中,路由是指将 URL 映射到特定函数的过程。Node.js 提供了多种方式来实现路由功能,我们可以使用内置模块 ` 或第三方库如 Express 来实现。

### 使用内置模块 ` 来实现一个简单的路由示例:

javascriptconst http = require(' />
// 创建 HTTP服务器const server =  res) => {
 // 获取 URL 的路径部分 const path = req.url;

 // 根据路径映射到不同的函数 if (path === '/') {
 res.writeHead(200, {'Content-Type': 'text/plain'});
 res.end('Hello World
');
 } else if (path === '/about') {
 res.writeHead(200, {'Content-Type': 'text/plain'});
 res.end('This is about page
');
 } else {
 res.writeHead(404, {'Content-Type': 'text/plain'});
 res.end('Not Found
');
 }
});

// 启动服务器server.listen(3000, () => {
 console.log('Server running at  />});


在这个示例中,我们使用 ` 方法创建了一个 HTTP服务器,并为不同的 URL 路径映射到不同的函数。

### 使用第三方库 ExpressExpress 是 Node.js 中最流行的 Web 框架之一,它提供了更强大的路由功能。我们可以使用 Express 来实现上述示例:

javascriptconst express = require('express');

// 创建 Express 应用const app = express();

// GET / 路径映射到 index 函数app.get('/', (req, res) => {
 res.send('Hello World');
});

// GET /about 路径映射到 about 函数app.get('/about', (req, res) => {
 res.send('This is about page');
});

// 启动服务器const port =3000;
app.listen(port, () => {
 console.log(`Server running at  />});


在这个示例中,我们使用 Express 来创建一个 Web 应用,并为不同的 URL 路径映射到不同的函数。

**获取参数**

在 Node.js 中,获取 URL 的参数可以通过 `req.query` 或 `req.params` 属性来实现。

### 使用 req.query我们可以使用 `req.query` 属性来获取 URL 的查询字符串参数:

javascriptconst express = require('express');

// 创建 Express 应用const app = express();

// GET / 路径映射到 index 函数app.get('/', (req, res) => {
 const name = req.query.name;
 const age = req.query.age;

 if (name && age) {
 res.send(`Hello ${name}, you are ${age} years old.`);
 } else {
 res.send('Please provide name and age.');
 }
});

// 启动服务器const port =3000;
app.listen(port, () => {
 console.log(`Server running at  />});


在这个示例中,我们使用 `req.query` 属性来获取 URL 的查询字符串参数。

### 使用 req.params我们可以使用 `req.params` 属性来获取 URL 的路径参数:

javascriptconst express = require('express');

// 创建 Express 应用const app = express();

// GET /:name 路径映射到 index 函数app.get('/:name', (req, res) => {
 const name = req.params.name;

 if (name) {
 res.send(`Hello ${name}.`);
 } else {
 res.send('Please provide name.');
 }
});

// 启动服务器const port =3000;
app.listen(port, () => {
 console.log(`Server running at  />});


在这个示例中,我们使用 `req.params` 属性来获取 URL 的路径参数。

**静态目录**

在 Node.js 中,静态目录可以通过内置模块 ` 或第三方库如 Express 来实现。

### 使用内置模块 ` 来实现一个简单的静态目录示例:

javascriptconst http = require(' />
// 创建 HTTP服务器const server =  res) => {
 // 获取 URL 的路径部分 const path = req.url;

 // 如果是静态文件,直接返回文件内容 if (path === '/static/index.html') {
 res.writeHead(200, {'Content-Type': 'text/html'});
 res.end('<h1>Hello World</h1>');
 } else {
 res.writeHead(404, {'Content-Type': 'text/plain'});
 res.end('Not Found
');
 }
});

// 启动服务器server.listen(3000, () => {
 console.log('Server running at  />});


在这个示例中,我们使用内置模块 ` 来实现一个简单的静态目录。

### 使用第三方库 Express我们可以使用第三方库 Express 来实现一个更强大的静态目录:

javascriptconst express = require('express');

// 创建 Express 应用const app = express();

// 静态目录app.use(express.static('static'));

// 启动服务器const port =3000;
app.listen(port, () => {
 console.log(`Server running at  />});


在这个示例中,我们使用 Express 来实现一个更强大的静态目录。

以上就是 Node.js 中原生路由、获取参数和静态目录的基本实现。

其他信息

其他资源

Top