Express: Express without template engine?

Created on 14 Apr 2016  路  23Comments  路  Source: expressjs/express

Hi,
I'm trying to do something with Express and angular, and a problem confused me for several days..

How to use Express without template engine? I mean , I just wanna use HTML files, as I wanna angular's route.js to control the HTML files sended to browser, not express render the HTMLs, like this:

route.js (angular)
$routeProvider.when( '/' , { templateUrl: '../../***.html', .... })

Then I needn't to write
res.render(...)
in express.route.

I found a solution like this on stackoverflow:
app.engine('html', require('ejs').renderFile)
app.set('view engine', 'ejs')

That's not what I want. I still need to write
res.render(' ***.html')
in my express.route.

And I don't wanna put these HTML files in express.static(), either. I don't think that's a good idea for angular.

So, How to use Express without template engine? Just use HTML files without set template engine?

Really hope your replies soon.

Most helpful comment

I agree that template engine should be able to set to none cleanly instead of what we have now, i.e. it should work as is with/without a view engine.

All 23 comments

Hi ,render is used to render the template engines so use res.sendfile(filename.html) instead or if you want to send html only you can do this app.get('thisroute', function(req, res) { res.type(text/html); res.status(200); res.send(<p>HELLO WORLD!);

Then how to write my app.js?
If I annotate app.engine(...) app.set('view engine', ...)

It will crashed and says: No default engine was specified and no extension was provided.

if you are using some kind of view engines you need to do app.engine, otherwise just make the route and send the file that you nedd or the html tag that you need

I just wanna jump the view engine... If I used the template engine, I have to provide an api for each route in my express like
route.get('/', function(req, res, next) {
res.render('....') };)
But I wanna express only provide RESTful api. Things like this will waste my GET.

I don't wanna using any kind of view engines. Just HTML.
But as lone as I don't write
app.set('view engine' .....) in my app.js, it will crashed and says No default engines.

Seems like it tell me : You MUST set view engines !

And it confused me quite much.

e....
Can you understand me ?

here is some simple server

var express = require("express"),
app = express();
app.get("theroutyouwant", function(req, res){
// if you need to send a html file
res.sendfile("filename.html");
//if you need to send html tag
res.type("text/html");
res.send("<p>hello world!</p>");
};
app.listen(3000);
console.log('server is running on port 3000);

this is simple server with out using view engines, but if you are using view engine it you will need to use app.engine and if you are not using view engines you don't need it

if that is not what you want can provider an example of your app.js

Hey... If it is convenient, can you leave an email for me?
I think I need more help

yeah that is ok can you tell me your emal i will send you an hour later am working now

[email protected] or [email protected]
Both is ok.

And my project is on this repo:
I will appreciate if you can check where the problem is and tell me by email . Thanks !

what is the project name

ok i will let you know in an hour

@THCloud you can serve static files as described on this page - http://expressjs.com/en/starter/static-files.html

@THCloud this doc describes how to serve static files - http://expressjs.com/en/starter/static-files.html

Keep the thread closed, but I now understand what the user was asking, as I had the same issue.
When you use the express generator, it forces you to select a view engine, or uses Jade by default. I prefer not to use any of those, and to simply stick to HTML.

I see in app.js now, that the line app.use(express.static(path.join(__dirname, 'public'))); allows HTML to work by default.

Simply place those files inside of your public folder, and you are good to go.

Also leaving the thread closed, but ultimately, this is how Express and Angular are supposed to work.

Install express as your underlying Node server, dealing with your GETs and POSTs back to the database (if you're MEAN, then MongoDB) and then use @ angular/cli to build a public folder (just change 'dist' to 'public' in the .angular-cli.json file) and after developing your Angular site, use 'ng build' to bundle all static files (your routes.js included) into that public folder, and then FTP all those files from the built 'public' folder to the public folder on your existing Express.js server.

@tramel-woodard
Great info here. Two quick questions, if you don't mind further elaborating.
1) How do you FTP the files in your built public folder to the public folder in your Express.js server?
2) When you say to use "ng-build to bundle all static files (routes.js included)" are you referring to the Express.js server's routes.js file or to any routes created in the Angular app? (just want to be sure I understood correctly, because I would have expected a .ts extension for Angular routes).

@Caoimhin89

  1. After building out the Angular application, I just open an FTP program and upload those files directly to the production server (dedicated server, shared hosting usually doesn't allow node.js servers).

This is where web hosting is different for everyone. For my hosting, all node.js servers come equipped with a default app.js file and a 'public' folder. Usually web hosts provide domains that have Node.js hosting with a standard 'public' folder in the root directory.

  1. Sort of a misstatement on my part, I meant any routes created in the Angular app.

Also, your comment regarding .ts extensions, ng build sort of works like the Java Compiler:

.java files --> (compiler) --> .class files --> (java machine) --> running application
.ts files --> (ng build) --> .js files --> (express.js) --> running web application

@tramel-woodard
Makes sense. Many thanks for the info!

@DaveVoyles , finally I do like what you say.
regard html files as static resource is a good way. and it works.

I agree that template engine should be able to set to none cleanly instead of what we have now, i.e. it should work as is with/without a view engine.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wxs77577 picture wxs77577  路  3Comments

snowdream picture snowdream  路  3Comments

extensionsapp picture extensionsapp  路  3Comments

zackarychapple picture zackarychapple  路  3Comments

despairblue picture despairblue  路  3Comments