forked from BYUCS260/node-api-server-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-server.js
67 lines (57 loc) · 1.56 KB
/
api-server.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* This array of pokemon will represent a piece of data in our 'database'
*/
var pokemon = [
{
name: 'Pikachu',
avatarUrl: 'http://rs795.pbsrc.com/albums/yy232/PixKaruumi/Pokemon%20Pixels/Pikachu_Icon__free__by_Aminako.gif~c200'
},
{
name: 'Charmander',
avatarUrl: 'http://24.media.tumblr.com/tumblr_ma0tijLFPg1rfjowdo1_500.gif'
},
{
name: 'Mew',
avatarUrl: 'http://media3.giphy.com/media/J5JrPT8r1xGda/giphy.gif'
},
{
name: 'Cubone',
avatarUrl: 'http://rs1169.pbsrc.com/albums/r511/nthndo/tumblr_ljsx6dPMNm1qii50go1_400.gif~c200'
},
{
name: 'Cleffa',
avatarUrl: 'http://media1.giphy.com/media/pTh2K2xTJ1nag/giphy.gif'
},
{
name: 'Gengar',
avatarUrl: 'https://s-media-cache-ak0.pinimg.com/originals/7e/3b/67/7e3b67c53469cc4302035be70a7f2d60.gif'
}
];
var http = require('http');
var url = require('url');
var fs = require('fs');
var ROOT_DIR = "src/";
var port = 4000;
http.createServer(function (req, res) {
var urlObj = url.parse(req.url, true, false);
if (urlObj.pathname === '/pokemon') {
/**
* TODO: return the array of pokemon above as a string
* with an header status of 'ok'
*/
} else {
/**
* Here is where we return all requests for files in our 'src' directory
*/
fs.readFile(ROOT_DIR + urlObj.pathname, function (err, data) {
if (err) {
res.writeHead(404);
res.end(JSON.stringify(err));
return;
}
res.writeHead(200);
res.end(data);
});
}
}).listen(port);
console.log('app is now running on port: ' + port)