(精華)2020年7月10日 Node.js express(router路由的使用)
可掛載路由
var express = require('express');
var router = express.Router(); //可掛載路由處理程式
// 路由器的中介軟體
router.use(function timeLog (req, res, next) {
console.log('Time: ', Date.now())
next()
})
// 首頁路由
router.get('/home', function (req, res) {
res.json({
home:'Birds home page'
})
})
// 關於我們
router.get('/about', function (req, res) {
res.json({
about:'About birds'
})
})
module.exports = router;
引用可掛在路由
const express = require('express');
const cors = require('cors');
const path = require('path');
const app = express();
// /* 自己封裝跨域中介軟體 */
// app.use('*',function (req, res, next) {
// res.header('Access-Control-Allow-Origin', '*'); //這個表示任意域名都可以訪問,這樣寫不能攜帶cookie了。
// //res.header('Access-Control-Allow-Origin', 'http://www.baidu.com'); //這樣寫,只有www.baidu.com 可以訪問。
// res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
// res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');//設定方法
// if (req.method == 'OPTIONS') {
// res.send(200); // 意思是,在正常的請求之前,會傳送一個驗證,是否可以請求。
// }
// else {
// next();
// }
// });
//使用cors跨域中介軟體
app.use(cors());
var birds = require('./routers/birds.js');
// app.Method(path,hander)
// METHOD: get,post,delete,put
//path : 路由
//hander:匹配到路由時 執行函式
// Express 在靜態目錄查詢檔案,因此,存放靜態檔案的目錄名不會出現在 URL 中。
// 如果要使用多個靜態資源目錄,請多次呼叫 express.static 中介軟體函式
// http://localhost:3003/index.html
app.use(express.static('public'))
app.use(express.static('public2'))
// 靜態函式,為靜態目錄指定一個掛載路徑,如下所示
// app.use('/static', express.static(path.join(__dirname, 'public')));
app.use('/html', express.static(path.join(__dirname, 'html')));
//訪問的連結 :http://localhost:3003/html/index.html
app.get('/', (req, res) => res.send('Hello World!'))
// app.post('/info', (req, res)=>{
// res.send('Hello World!')
// })
// app.get('/info', (req, res)=>{
// res.send('Hello World!')
// })
// app.delete('/info', (req, res)=>{
// res.send('Hello World!')
// })
// app.put('/info', (req, res)=>{
// res.send('Hello World!')
// })
app.use('/birds', birds);
app.route('/info')
.get(function (req, res) {
res.send('Get a random book')
})
.post(function (req, res) {
res.send('Add a book')
})
.put(function (req, res) {
res.send('Update the book')
})
app.listen(3003, () => console.log('Example app listening on port 3003!'))
相關文章
- (精華2020年5月8日更新) vue教程篇 vue-router路由的使用Vue路由
- (精華2020年5月8日更新) vue教程篇 vue-router路由的許可權控制Vue路由
- (精華)2020年7月14日 vue vue-router動態路由的實現許可權控制Vue路由
- Node.js學習之路27——Express的router物件Node.jsExpress物件
- (精華)2020年7月17日 vue mixins的使用Vue
- (精華)2020年8月2日 TypeScript 裝飾器的使用TypeScript
- (精華)2020年9月2日 .NET Core 命令列的基本使用命令列
- (精華2020年5月22日更新) react基礎篇 元件的使用React元件
- (精華)2020年10月3日 微服務 Docker-叢集(swarm)微服務DockerSwarm
- (精華)2020年7月20日 ASP.NET Core serilog日誌框架的使用ASP.NET框架
- (精華)2020年7月20日 ASP.NET Core log4.net日誌框架的使用ASP.NET框架
- (精華)2020年6月28日 Canvas 進度條Canvas
- (精華)2020年8月18日 快取機制快取
- (精華)2020年10月4日 微服務 k8s部署專案微服務K8S
- (精華)2020年8月22日 ABP vNext DTO在應用層的使用
- (精華)2020年6月28日 Canvas 基礎知識Canvas
- (精華)2020年7月18日 vue clipboard複製剪下Vue
- (精華)2020年7月1日 ASP.NET Core Swagger的使用(Swashbuckle工具版)ASP.NETSwagger
- (精華2020年5月4日更新) vue教程篇 計算屬性computed的使用Vue
- (精華2020年5月4日更新) vue教程篇 v-show和v-if的使用Vue
- (精華)2020年6月26日 C#類庫model OptionListInputDTOC#
- (精華)2020年6月26日 C#類庫model RoleTypesC#
- (精華)2020年6月26日 C#類庫model DbTableInfoC#
- (精華)2020年6月26日 C#類庫model AjaxResultC#
- (精華)2020年6月26日 C#類庫model ErrorResultC#Error
- (精華)2020年6月26日 C#類庫model TableInfoC#
- (精華)2020年6月26日 C#類庫model BusExceptionC#Exception
- (精華)2020年6月26日 C#類庫model PageResultC#
- (精華)2020年6月26日 C#類庫model PageInputC#
- (精華)2020年7月22日 ASP.NET Core Swagger的使用(NSwag工具版)ASP.NETSwagger
- (精華)2020年7月1日 ASP.NET Core 使用Enablebuffering多次讀取bodyASP.NET
- vue 路由vue RouterVue路由
- Node.js Express 框架Node.jsExpress框架
- 使用react-router-config配置路由React路由
- 淺談Vue-router使用方法及動態路由和巢狀路由的使用Vue路由巢狀
- (精華)2020年6月26日 C#類庫model IdInputDTOC#
- (精華)2020年6月26日 C#類庫model UserLogTypeC#
- (精華)2020年6月26日 C#類庫model FileEntryC#