MEAN.JS Express
本章演示了使用
Node 和
Express 为应用程序构建路由。
在上一章中,我们创建了一个 node-express 应用程序。导航到名为
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}!`));
运行应用程序
接下来,使用以下命令运行应用程序-
您将收到如下图所示的确认-
现在,转到浏览器并输入
http://localhost:3000/myroute。您将获得如下图所示的页面-
