2018-03-13 21:25:39 +01:00
|
|
|
// server.js
|
|
|
|
// where your node app starts
|
|
|
|
|
2020-02-19 23:18:49 +01:00
|
|
|
// we've started you off with Express (https://expressjs.com/)
|
|
|
|
// but feel free to use whatever libraries or frameworks you'd like through `package.json`.
|
2019-09-30 23:05:06 +02:00
|
|
|
const express = require("express");
|
2018-10-15 17:28:02 +02:00
|
|
|
const app = express();
|
2018-03-13 21:25:39 +01:00
|
|
|
|
2020-02-19 23:18:49 +01:00
|
|
|
// our default array of dreams
|
|
|
|
const dreams = [
|
|
|
|
"Find and count some sheep",
|
|
|
|
"Climb a really tall mountain",
|
|
|
|
"Wash the dishes"
|
|
|
|
];
|
2018-03-13 21:25:39 +01:00
|
|
|
|
2020-02-19 23:18:49 +01:00
|
|
|
// make all the files in 'public' available
|
|
|
|
// https://expressjs.com/en/starter/static-files.html
|
2019-09-30 23:05:06 +02:00
|
|
|
app.use(express.static("public"));
|
2018-03-13 21:25:39 +01:00
|
|
|
|
2020-02-19 23:18:49 +01:00
|
|
|
// https://expressjs.com/en/starter/basic-routing.html
|
|
|
|
app.get("/", (request, response) => {
|
2019-09-30 23:05:06 +02:00
|
|
|
response.sendFile(__dirname + "/views/index.html");
|
2018-03-13 21:25:39 +01:00
|
|
|
});
|
|
|
|
|
2020-02-19 23:18:49 +01:00
|
|
|
// send the default array of dreams to the webpage
|
|
|
|
app.get("/dreams", (request, response) => {
|
|
|
|
// express helps us take JS objects and send them as JSON
|
|
|
|
response.json(dreams);
|
|
|
|
});
|
|
|
|
|
2018-03-13 21:25:39 +01:00
|
|
|
// listen for requests :)
|
2020-02-19 23:18:49 +01:00
|
|
|
const listener = app.listen(process.env.PORT, () => {
|
2019-09-30 23:05:06 +02:00
|
|
|
console.log("Your app is listening on port " + listener.address().port);
|
2018-03-13 21:25:39 +01:00
|
|
|
});
|