Goal: Create a test website and an Express server to display the website pages on localhost.
Your site will display pages for these sample routes:
/ /contact_us /specials /eiffel_towerIt will also display a route not found page if a route is entered other than these four. The HTML pages that correspond to these sample routes are index.html, contact-us.html, specials.html, eiffel-tower.png, and route-not-found.html.
> npm i express
const app = express( );
app.get("/", (req, res) => {
res.sendFile(path.join(
__dirname + '/index.html'));
});
If you prefer to put the HTML files in the views folder,
set up the route like this:
app.get("/", (req, res) => {
res.sendFile(path.join(
__dirname + '/views/index.html'));
});
app.get("*", (req, res) => {
res.sendFile(path.join(
__dirname + '/file-not-found.html'));
});
const PORT = 3000;
app.listen(PORT, ( ) => {
console.log("Server started; " +
`listening on port ${PORT}.`);
});
Notice that, in addition to the port argument,
the Express app object also accepts a callback function
that is called when the app starts listening.Finish Tutorial 4 by implementing an external stylesheet for all HTML pages, and including at least one image on one or more HTML pages.
app.use(express.static('public'));
<p>
<a href="/contact_us">Contact Us</a>
<a href="/specials">Specials</a>
<a href="/eiffel_tower">Eiffel Tower</a></p>
</p>
<head>
<title>Contact Us</title>
<link rel="stylesheet" href="styles.css">
</head>