I'm encountering difficulties with using express. I am trying to launch by typing
localhost:3000/timer -> timetimer.html
, but it's not functioning as expected.
server.js
:
var express = require('express');
var path = require('path');
var indexRouter = require('./routes/index');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use(function(req,res,next){
res.status(404).send('Unknown Request');
next();
});
app.listen(function () {
console.log('Server listening on port 3000!');
});
module.exports = app;
index.js
:
var express = require('express');
var path = require('path');
var router = express.Router();
router.get('/main', function(req,res){
res.sendFile('/main.html', {root: path.join(__dirname,'../public')});
});
router.get('/timer', function(req,res,next){
//res.sendFile('public/timetimer.html', {root: path.join(__dirname,'../public')});
res.sendFile(__dirname + "/timetimer.html")
});
module.exports = router;
Folder structure:
https://i.sstatic.net/8Xhnk.png
The code launches successfully, showing that server.js
is listening on port 3000. However, when I enter localhost:3000/timer
in the browser, the site cannot be reached.