var express = require('express');
var bodyParser = require('body-parser')
var app = express();
var low = require('lowdb');
var db = low('db.json');

app.use(bodyParser.json());// 解析application/json
app.use(bodyParser.urlencoded({ extended: true }));//解析從網頁送來的資料
app.use(express.static('public'));

//初始化lowdb的db.json檔
db.defaults({ secrets: [] }).write();

//========================================================

app.get('/', function (req, res) {
    res.sendFile(__dirname + "/views/index.html");
});

//========================================================

app.post('/secret', function (req, res) {

    var newSecret =
        db.get('secrets')
            .push(req.body.secret)
            .write();

    var secret = 
        db.get('secrets')
            .sample()
            .value();

    res.send({secret:secret});
});

//========================================================

app.listen(3000, function () {
    console.log("伺服器跑起來啦!在你的瀏覽器網址列輸入 localhost:" + 3000 + " 吧!");
});

results matching ""

    No results matching ""