MEAN.JS教程

MEAN.JS Express

本章演示了使用 NodeExpress 为应用程序构建路由。
在上一章中,我们创建了一个 node-express 应用程序。导航到名为 mean-demo 的项目目录。使用以下命令转到目录-
$ cd mean-demo

设置路由

通过使用传入请求的 URL 将路由用作映射服务。打开 server.js 文件并设置路由,如下所示-
// modules =================================================
const express = require('express');
const app = express();
// set our port
const port = 3000;
app.get('/', (req, res) ⇒ res.send('Welcome to Tutorialspoint!'));
//defining route
app.get('/tproute', function (req, res) {
   res.send('this is routing for the application developed using Node and Express...');
});
// startup our app at http://localhost:3000
app.listen(port, () ⇒ console.log(`Example app listening on port ${port}!`));

运行应用程序

接下来,使用以下命令运行应用程序-
$ npm start
您将收到如下图所示的确认-
运行应用程序
现在,转到浏览器并输入 http://localhost:3000/myroute。您将获得如下图所示的页面-
Node Express
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4