-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
49 lines (34 loc) · 1.58 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
/*
Node + Express Example code for CS160 Summer 2023
Prepared by Shm Garanganao Almeda for Summer 2022
Code referenced from:
https://www.digitalocean.com/community/tutorials/how-to-create-a-web-server-in-node-js-with-the-http-module"
https://expressjs.com/en/starter/hello-world.html
https://codeforgeek.com/render-html-file-expressjs/
https://stackoverflow.com/questions/32257736/app-use-express-serve-multiple-html
*/
//Node modules to *require*
//if these cause errors, be sure you've installed them, ex: 'npm install express'
const path = require('path');
const express = require('express');
const app = express();
//specify that we want to run our website on 'http://localhost:8000/'
const host = 'localhost';
const port = 8000;
var publicPath = path.join(__dirname, 'public'); //get the path to use our "public" folder where we stored our html, css, images, etc
app.use(express.static(publicPath)); //tell express to use that folder
//here's where we specify what to send to users that connect to our web server...
//if there's no url extension, it will show "index.html"
app.get('/', function (req, res) {
res.sendFile(publicPath + '/');
});
//depending on what url extension the user navigates to, send them the respective html file.
/* // this example app has no additional pages besides the index, so I've commented these out for now.
app.get('/a', function (req, res) {
res.sendFile(publicPath + '/a.html');
});
*/
//run this server by entering "node App.js" using your command line.
app.listen(port, () => {
console.log(`Server is running on http://${host}:${port}`);
});