Exploring and Thinking

網址傳送資料給 Node.js (app.get)

Node.js 需安裝 express 模組。 (sudo npm install express)

再撰寫以下程式碼,檔案自訂。

App.js

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
var express = require('express');
var app = express();

app.get('/data', function(req, res) {
    console.log(req.query.name);
    console.log(req.query.country);
    res.send('Name:' + req.query.name + '<br />' +'Country:' + req.query.country);
    res.end();

});


app.listen(12345);

Terminal 執行 node app.js

使用瀏覽器,網址輸入 http://127.0.0.1:12345/data?name=Ian&country=Taipei

Terminal 會顯示網址所帶的參數:

Ians-MBP:Desktop ian$ node app.js
Ian
Taipei

網頁顯示如下:

Share:

熱門文章

標籤