-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path4_express-server-static.js
More file actions
36 lines (28 loc) · 970 Bytes
/
4_express-server-static.js
File metadata and controls
36 lines (28 loc) · 970 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
const express = require('express');
const app = express();
var port = 3002;
/*
You can tell express where you are storing your static files.
In this example, we are storing these files within the 'public'
folder, and we are telling express to treat those files as if
they are on the root level.
*/
app.use(express.static('public'));
/*
Now we can create responses using HTML files we store in the public
folder.
*/
app.get('/', function(request, response) {
// You can use sendFile() to quickly serve an html file with your
// response. Since we set up the server to serve static files from
// our 'public' folder, we can simply give it the name of the file
// within that folder.
response.sendFile('index.html');
});
app.get('/myURI', function(request, response) {
response.send('Responding to a GET request!');
});
app.post('/myURI', function(request, response) {
response.send('Responding to a POST request!');
});
app.listen(port);