-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
51 lines (37 loc) · 893 Bytes
/
server.js
File metadata and controls
51 lines (37 loc) · 893 Bytes
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
var express = require('express')
var app = express();
var fs=require('fs')
app.listen(8000)
var stored={}
stored.allTweets=[]
app.use(express.static(__dirname+'/public'))
//Retrieve previous tweets
fs.readFile(__dirname+"/public/tweet.JSON",function(er,data){
if(er)
conole.log(er)
else
stored.allTweets.push((JSON.parse(data)))
})
app.post('/send', express.bodyParser(), function(req, res)
{
console.log("RECD", req.body);
if (req.body)
{
stored.allTweets.push(req.body)
fs.writeFile(__dirname+"/public/tweet.JSON",JSON.stringify(stored.allTweets)+"\r\n",function(er){//dirname
if(er)
console.log(er)
else
console.log("Tweet saved")
})
res.send({status:"ok", message:"Tweet received"})
}
else
{
//no tweet?
res.send({status:"nok", message:"No tweet received"})
}
})
app.get('/tweets', function(req,res) {
res.send(tweets)
})