-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe01.js
39 lines (29 loc) · 963 Bytes
/
e01.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const path = require('path')
const dbPath = path.resolve(__dirname, 'demodb02')
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database(dbPath);
db.serialize(function() {
db.run("CREATE TABLE IF NOT EXISTS counts (key TEXT, value INTEGER)");
db.run("INSERT INTO counts (key, value) VALUES (?, ?)", "counter", 0);
});
var express = require('express');
var restapi = express();
restapi.get('/data', function(req, res){
db.get("SELECT value FROM counts", function(err, row){
res.json({ "count" : row.value });
});
});
restapi.post('/data', function(req, res){
db.run("UPDATE counts SET value = value + 1 WHERE key = ?", "counter", function(err, row){
if (err){
console.err(err);
res.status(500);
}
else {
res.status(202);
}
res.end();
});
});
restapi.listen(3000);
console.log("Submit GET or POST to http://localhost:3000/data");