Killon
Killon

Reputation: 31

Express res.sendFile not doing anything

I'm having some problems with express, I want to launch, by doing localhost:3000/timer -> timetimer.html, but it isn't working.

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('Richiesta Sconosciuta');
    next();
});

app.listen(function () {
  console.log('Server in ascolto sulla porta 3000!');
});

module.exports = app;

index.js:

var express = require('express');
var path = require('path');
var router = express.Router();

router.get('/principale', function(req,res){
    res.sendFile('/principale.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;

Structure of the folder:

The launch of the code works, i.e. it says that server.js is listening on 3000, but when I write localhost:3000/timer the browser is unable to reach the site

Upvotes: 0

Views: 47

Answers (2)

Quentin
Quentin

Reputation: 943097

__dirname is the directory name of the current module.

So it is the routes folder.

timetimer.html isn't in the routes folder, so you're passing it a non-existent path.

Upvotes: 0

crystalbit
crystalbit

Reputation: 704

you didn't specify port in app.listen

try:

app.listen(3000, function () {
  console.log('Server in ascolto sulla porta 3000!');
});

Upvotes: 1

Related Questions