forked from shippableSamples/sample_node_sqlite
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·41 lines (37 loc) · 1.01 KB
/
index.js
File metadata and controls
executable file
·41 lines (37 loc) · 1.01 KB
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
var express = require("express"),
sqlite3 = require('sqlite3').verbose(),
app = express();
/* Prepping SQLite */
var db = new sqlite3.Database(':memory:');
db.run("DROP TABLE IF EXISTS Foo", function(err, row) {
if (err) { console.log(err); }
});
db.run("CREATE TABLE Foo(name TEXT)", function(err, row) {
if (err) { console.log(err); }
});
/* Routes */
app.get("/", function (req, res) {
res.send("Hey buddy!");
});
app.get("/:name", function (req, res) {
db.get("SELECT name FROM Foo", function (err, row) {
console.log(err);
if (row === undefined) {
db.serialize(function() {
db.run("INSERT INTO Foo(name) VALUES('" + req.params.name + "')", function(err, row) {
if (err) {
res.send(500);
} else {
res.send("Created a new thing with name " + req.params.name);
}
});
});
} else {
res.send(row);
db.close();
}
});
});
app.listen(3000, function () {
console.log('Express listening on port 3000');
});