-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (70 loc) · 2.63 KB
/
app.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
67
68
69
70
// get the http module:
var http = require('http');
// fs module allows us to read and write content for responses!!
var fs = require('fs');
// creating a server using http module:
var server = http.createServer(function (request, response){
// see what URL the clients are requesting:
console.log('client request URL: ', request.url);
// this is how we do routing:
if(request.url === '/cars') {
fs.readFile('./views/index.html', 'utf8', function (errors, contents){
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(contents);
response.end();
});
}
else if(request.url === '/images/car1.jpeg'){
// notice we won't include the utf8 encoding
fs.readFile('./images/car1.jpeg', function(errors, contents){
response.writeHead(200, {'Content-type': 'image/jpg'});
response.write(contents);
response.end();
})
}
else if(request.url === '/images/car2.jpeg'){
// notice we won't include the utf8 encoding
fs.readFile('./images/car2.jpg', function(errors, contents){
response.writeHead(200, {'Content-type': 'image/jpg'});
response.write(contents);
response.end();
})
}
else if (request.url === "/cats") {
fs.readFile('./views/cats.html', 'utf8', function (errors, contents){
response.writeHead(200, {'Content-type': 'text/html'});
response.write(contents);
response.end();
});
}
else if(request.url === '/images/cats.jpeg'){
// notice we won't include the utf8 encoding
fs.readFile('./images/cats.jpeg', function(errors, contents){
response.writeHead(200, {'Content-type': 'image/jpg'});
response.write(contents);
response.end();
})
}
else if (request.url === "/cars/new") {
fs.readFile('./views/cars.html', 'utf8', function (errors, contents){
response.writeHead(200, {'Content-type': 'text/html'});
response.write(contents);
response.end();
});
}
else if(request.url === '/stylesheets/style.css'){
fs.readFile('./stylesheets/style.css', 'utf8', function(errors, contents){
response.writeHead(200, {'Content-type': 'text/css'});
response.write(contents);
response.end();
})
}
// request didn't match anything:
else {
response.end('URL requested is not available.');
}
});
// tell your server which port to run on
server.listen(6789);
// print to terminal window
console.log("Running in localhost at port 6789");