0
 app.get('/',function(req,res,next){
       app.use(express.static(html file);
       next();
    });

app.get('/:someText',function(req,res){
var x = req.params.someText;
res.send(x);
});

I am getting the output for both, but nothing getting the CSS for that express.static(html) file.

Rohan
  • 129
  • 3
  • 7

2 Answers2

2

As i can see from code you missing send in get / .

app.get('/',function(req,res,next){
   //something
   //in here add res.send() and all OK.
});

Check this to understand what next is for.

What is the parameter "next" used for in Express?

Mykola Borysyuk
  • 3,373
  • 1
  • 18
  • 24
0

The way you're using express.static isn't right. You shouldn't pass it a single file to return, that's sendFile. express.static is used to serve up a entire directory and should be called outside the get handler.

For example, this would serve up a directory called public at the root of your URL. Any requests for files that aren't found would be passed on through the middleware/router chain to the next handler:

app.use(express.static(path.join(__dirname, 'public')));

Importantly this should appear before your calls to app.get, app.post, etc. and not inside a handler.

So if you've got a file at public/myfile.html that would be served up at http://localhost:3000/myfile.html, where I've assumed your server is at localhost:3000. If you wanted to add an extra section of path to the URL, e.g. http://localhost:3000/stat/myfile.html that would be:

app.use('/stat', express.static(path.join(__dirname, 'public')));

If you wanted to serve up a single file then you could use sendFile, a bit like this:

app.get('/myfile.html', function(req, res) {
    res.sendFile(path.join(__dirname, '/myfile.html'));
});

Note that this is singling out a particular file so any resources such as CSS would need to be handled separately. If the HTML, CSS, etc. are all in the same folder it would make sense to use express.static to serve up the entire directory instead.

It is also worth noting that express.static has a setting called index that defaults to serving up a file called index.html if a request comes in for '/'.

Further reading:

skirtle
  • 27,868
  • 4
  • 42
  • 57