0

I'm new with ExpressJS, I have a small example using EJS, but I want use AngularJS for manipulating the DOM. Both technologies offer me DOM manipulation, so why some people use it together? I don't understand.

I have no idea how to render HTML files. What is the correct way to use AngularJS with ExpressJS?

res.render('index.html');

2 Answers 2

2

As far as I know EJS is really for templating, none of the embedded js is executed on the client.

Personally I have found some cases where it is handy to use a templating language with AngularJS or any client side framework. For example, sometimes it is nice to be able to interpolate some csurf tokens, or session data required by the client app into your html on the server. Other times it is not necessary.

As for rendering html, use the express.static middleware. It comes with Express, you pass a file path, and it returns a handler that will serve the contents of a given directory. You could put express.static anywhere in your middleware chain, but I usually put it at the top to avoid naming issues. Read the documentation for more information.

Consider the following:

 var express = require('express')
 var app = express();
 // Asssuming we have a directory 
 // `public` in the root of the application
 app.use('/', express.static(__dirname + '/public'));
 // now you're serving all the files in public directory off the root path
// Add middlewares and routes...

For a really simple app you could you use the fs module, and stream the contents of a file to the response. The following is naive example of how you could do this, in production you would want to listen for error events and implement some cache control.

  app.get('/', function(req, res) {
     res.set('Content-Type', 'text/html');
     fs.createReadStream('./index.html').pipe(res);
  });
Sign up to request clarification or add additional context in comments.

Comments

0

Please use this :to render first time and for more information please go her http://www.tutorialspoint.com/nodejs/nodejs_express_framework.htm

var express = require('express');
var app = express();
 app.get('/', function (req, res) {
 res.sendFile( __dirname + "/" + "index.html" );
})

Comments

Your Answer

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