0

I have a very simple express app that has two routes, one that should render html, and one that should render a javascript file. Ideally, I'd like to have each in different folders, a ./views folder for html and an ./assets/javascripts folder for js. I'm wondering the best way to do this? Here is what I have so far:

var express = require('express')
  , app     = express()
  , fs      = require('fs')

['html', 'js'].forEach(function(extension) {
  app.engine(extension, function(filePath, options, callback) {
    fs.readFile(filePath, function(err, content) {
      if (err) throw new Error(err)
      return callback(null, content.toString())
    })
  })
})

app.set('views', './views')
app.set('view engine', 'html')

app.get('/', function(req, res) {
  res.render('index')
})

app.listen(3000)

Do I need to be doing this? Where can I find more information?

1
  • where is your second route??? And you don't need to have routes for static javascript files!!! Commented Oct 18, 2014 at 13:20

2 Answers 2

1

If you are just trying to make routes that respond to a static file. Use:

app.get('/', function(req, res) {
    res.sendFile(__dirname + '/path-to/your/static-file');
});

If you just want to set assets for your project. You should do:

app.use(express.static(__dirname + '/public'));

And then use a dedicated public directory for your assets.

(If this wasn't the answer you were looking for ask me in comments what you want!)

Sign up to request clarification or add additional context in comments.

Comments

1

For static JavaScript files, you just need to set your public assets folder, as such:

app.use(express.static(__dirname + '/public'));

I suggest you read the API documentation further: http://expressjs.com/api.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.