Here is my js file, which is in the same directory of my html file , I want to render it.
How hard can it be? I don't know what I am missing.
var express = require('express');
var app = express();
app.engine('html',require('ejs').renderFile);
app.set('views', __dirname + '/'); //Edit #1
app.use(express.directory('public'));
app.use(express.static('public'));
app.get('/', function(req, res){
//res.send('hello world');
res.render('gapijs.html');
});
app.listen(8090);
I used express.directory and express.static after reading expressjs docs(I am not sure it is right). I have all the deps installed and firmly linked in that directory. (using npm link )
Edit: I got to set views as mentioned in the comments by @Stan After that, I got the same effect of res.write in vanilla node.
Here is another problem I am facing,How to deploy js and css files which are needed by gapijs.html and do I have to do anything to take care of js which I am downloading everytime I load the webpage?
Edit 2: here is my final version of code(which works the way I want).
var express = require('express');
var app = express();
app.engine('html',require('ejs').renderFile);
app.set('views', __dirname + '/public');
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res){
//res.send('hello world');
res.render('gapijs.html');
});
app.listen(8090);
gapijs.html is in /public and js files public/js and css in public/css , the way js and css files are placed, when generating project using express command.
refer to those files from html as src="/js/blabla.js"
You need to set your views to be something like this:
app.set('views', __dirname + '/');
You can put the JS and CSS files in the /js and /css in public directory and reference them as follows, (as it is the convention):
<script src="/js/jquery.js"></script>
This should solve your issues. Good Luck