I’m making a site with express using the node app template. I am not able to make multiple pages like www.site.glitch.me/home or www.site.glitch.me/about. I’ve tried and it won’t work.
Bump((fillercontenthere))
There’s a page on using routing in Express here: https://expressjs.com/en/guide/routing.html
But using the node-app template you could add routes for your home and about pages like so:
var express = require('express');
var app = express();
// we've started you off with Express,
// but feel free to use whatever libs or frameworks you'd like through `package.json`.
// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));
// http://expressjs.com/en/starter/basic-routing.html
app.get('/', function(request, response) {
response.sendFile(__dirname + '/views/index.html');
});
// http://expressjs.com/en/starter/basic-routing.html
app.get('/home', function(request, response) {
response.sendFile(__dirname + '/views/home.html');
});
// http://expressjs.com/en/starter/basic-routing.html
app.get('/about', function(request, response) {
response.sendFile(__dirname + '/views/about.html');
});
// listen for requests :)
var listener = app.listen(process.env.PORT, function() {
console.log('Your app is listening on port ' + listener.address().port);
});
You would need to create the home.html and about.html files in the views
directory.
Note that the site would be accessible at site.glitch.me and not www.site.glitch.me as we don’t make the www sub-sub-domain available to sub-domains.
Looks like www.site.glitch.me redirects to site.glitch.me
I’m really sorry to bring this post back up, but I tried doing this and it just said ‘Cannot GET /filenamehere/index.html’. Is there any way to stop this happening?
You might want to try going to /filename or /filename/. There is not need for index.html is you are using Express.
Thankyou! This was very helpful.