跳至主要内容

Web Backend 2

installing
npm install --save-dev nodemon 
"dev": "nodemon ./bin/www"
extends layout

block content
h1= title
p Welcome to #{title}
form (action='/login', method='post')
input (type='text', name='username')
input (type='password', name='password')
input (type='submit', value='Login')

Express


Route


基礎架構

var express = require('express');
var app = express();

// respond with "hello world" when a GET request is made to the homepage
app.get('/', function(req, res) {
res.send('hello world');
});

路由方法

// GET method route
app.get('/', function (req, res) {
res.send('GET request to the homepage');
});

// POST method route
app.post('/', function (req, res) {
res.send('POST request to the homepage');
});

回應方法


方法說明
res.download()提供要下載的檔案
res.end()結束回應程序
res.json()傳送 JSON 回應
res.jsonp()傳送 JSON 回應,並支援 JSONP。
res.redirect()將要求重新導向
res.render()呈現視圖範本
res.send()傳送各種類型的回應
res.sendFile()以八位元組串流形式傳送檔案
res.sendStatus()設定回應狀態碼,並以回應內文形式傳送其字串表示法