MEAN.JS - 構建 Node 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 https://:3000
app.listen(port, () ⇒ console.log(`Example app listening on port ${port}!`));

執行應用程式

接下來,使用以下命令執行該應用程式 -

$ npm start

你會收到一則確認,如下面的圖片所示 -

Running Application

現在,轉到瀏覽器並輸入https://:3000/myroute。你會得到如下圖所示的頁面 -

Node Express
廣告