MEAN.JS - 构建静态路由 Node Express

本章演示如何使用 NodeExpress 为应用程序构建路由。

在上一章中,我们创建了一个 node-express 应用程序。导航到名为 mean-demo 的项目目录。使用以下命令转到目录 −

$ cd mean-demo

设置路由

路由通过使用传入请求的 URL 用作映射服务。打开 server.js 文件并设置路由,如下所示 −

// 模块 ====================================================
const express = require('express');
const app = express();

// 设置我们的端口
const port = 3000;
app.get('/', (req, res) ⇒ res.send('Welcome to Tutorialspoint!'));

//定义路由
app.get('/tproute', function (req, res) {
res.send('这是使用 Node 和 Express 开发的应用程序的路由...');
});

// 在 http://localhost:3000 启动我们的应用程序
app.listen(port, () ⇒ console.log(`示例应用程序在端口 ${port} 上监听!`));

运行应用程序

接下来,使用以下命令运行应用程序 −

$ npm start

您将获得如下图所示的确认信息 −

运行应用程序

现在,转到浏览器并输入 http://localhost:3000/myroute。您将获得如下图所示的页面 −

Node Express