Wednesday, 19 April 2017

express.static dosnt point to the right path

Hi i get a problem every time i try to reach /about

this code works and all is fine

var express = require('express');  
var path = require('path');

var app = express();                 // define our app using express

var port = process.env.PORT || 3000;        // set our port

console.log(path.join(__dirname, 'public'));



app.get('/', function(req, res) {
    res.sendFile(path.join(__dirname, 'public')+'/index.html');   
 })
.get('/about', function(req, res) {
    console.log(__dirname);
     res.sendFile(path.join(__dirname, 'public')+'/about.html');   
 })
.get('/signIn', function(req, res) {
    res.sendFile(path.join(__dirname, 'public')+'/signIn.html');   
});

app.listen(port);
console.log('Magic happens on port ' + port);

but when i try to place the public folder in express.static i get an error

"Error: ENOENT: no such file or directory, stat 'C:\about.html' at Error (native)"

var port = process.env.PORT || 3000;        // set our port
app.use(express.static(path.join(__dirname, 'public')));
console.log(path.join(__dirname, 'public'));



app.get('/', function(req, res) {
    res.sendFile('/index.html');   
})
.get('/about', function(req, res) {
    console.log(__dirname);
    res.sendFile('/about.html');   
})
.get('/signIn', function(req, res) {
    res.sendFile('/signIn.html');   
});


app.listen(port);
console.log('Magic happens on port ' + port);



via VodkaPlease

No comments:

Post a Comment