-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (36 loc) · 1.15 KB
/
index.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
40
41
42
43
var express = require('express');
var app = express();
const sqlite3 = require('sqlite3').verbose();
var server = app.listen(process.env.PORT || 3000, listen);
let counter = 0;
app.use(express.static('Public'));
app.post('/count', function(req,res,next){
let user = req.headers.user || null;
if(user){
db.run(`INSERT INTO users(user) VALUES(?)`, [user], function(err) {
if (err) {
return console.log(err.message);
}
counter++;
console.log(`${user} has been inserted with rowid ${this.lastID}`);
});
}
console.log(counter);
res.sendStatus(200);
});
function listen() {
var host = server.address().address;
var port = server.address().port;
console.log('Started server at https://' + host + ':' + port);
}
let db = new sqlite3.Database('./db/data.db', (err) => {
if (err) {
console.error(err.message);
}
console.log('Connected to the database.');
});
db.run(`CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
dt DATETIME DEFAULT CURRENT_TIMESTAMP,
user TEXT NOT NULL
);`);