-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.js
More file actions
51 lines (40 loc) · 1.33 KB
/
server.js
File metadata and controls
51 lines (40 loc) · 1.33 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
42
43
44
45
46
47
48
49
50
51
var express = require('express'),
app = express(),
router = express.Router(),
bodyParser = require('body-parser'),
path = require('path'),
sqlite = require('sqlite-sync');
sqlite.connect('./model/database.db');
sqlite.run("CREATE TABLE pessoas (id INTEGER PRIMARY KEY AUTOINCREMENT, nome CHAR(100), email CHAR(100));");
var exec = require('child_process').exec;
app.use(bodyParser.json());
app.use('/app', express.static(path.join(__dirname,'/app')));
app.use('/bootstrap', express.static(path.join(__dirname,'/bootstrap')));
app.use('/angular2', express.static(path.join(__dirname,'/angular2')));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.get('/api/pessoas', function(req,res){
res.send(sqlite.run("SELECT * FROM pessoas"));
});
app.post('/api/pessoas', function(req, res){
sqlite.insert('pessoas', req.body, function(id){
res.send({'id':id});
});
});
app.put("/api/pessoas/:id", function(req, res){
var id = req.params.id;
var body = req.body;
sqlite.update('pessoas', body, {id:id}, function(result){
res.send({result : result});
});
});
app.delete('/api/pessoas/:id', function(req, res){
var id = req.params.id;
sqlite.delete('pessoas',{id: id}, function(result){
res.send({result : result});
});
});
app.listen(3000, function(){
exec('start http://localhost:3000');
});