NodeJS: Serving dynamic files

0 votes
I have been using PHP, where codes are put in a file and executed each time while loading.

With NodeJS, I need to use HTML files. A way to do it would be putting the whole file's HTML content into the file that is running the HTTP server, but I'd like to have them in files instead.

I am using NodeJS and Express. How to do it?
Jul 16 in Others by shubham
• 6,890 points

1 answer to this question.

0 votes

You must use the EJS (Embedded JavaScript template) as your template engine.along with express to render the HTML files.

So to use in your application include the below lines in your application.js file 

var express = require('express');
var app = express();

app.set("view engine", "ejs");
app.set("views", __dirname + "/views");
app.set("view options", { layout: false } );

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

console.log('listening on port 3000...');
and then create a folder named views under which create a file named "index.ejs"
html using ejs

then start your server with node server.js and type localhost:3000 in the browser 
express with ejs
answered Jul 16 by sunshine
• 1,240 points

Related Questions In Others

0 votes
1 answer

Get files in my own file format for its own dynamic icon

Regarding MAC support, "The Mac and Windows ...READ MORE

answered Jan 8 in Others by DataKing99
• 8,130 points
0 votes
1 answer

Where can I find the details of installed files?

The details of recently and locally installed ...READ MORE

answered Mar 8 in Others by Nabarupa
+1 vote
0 answers

where to put tf files while building servers from terraform.. its creating only workspace.

# kitchen create -----> Starting Kitchen (v2.2.5) -----> Creating ...READ MORE

Aug 3 in Others by Priyanka
0 votes
1 answer

Nodejs: How to pass user form data into defined class

This should do your work: // select the ...READ MORE

answered Sep 19, 2018 in Blockchain by digger
• 26,600 points
0 votes
1 answer

how to safely deploy npm install without it causing inconsistencies?

The recent versions on npm generates a ...READ MORE

answered Apr 11, 2018 in DevOps on Cloud by DareDev
• 6,810 points
+1 vote
1 answer

Simple Unit Testing NodeJS/Express

You can test it with mocha which ...READ MORE

answered Jul 15 in Others by sunshine
• 1,240 points
0 votes
1 answer

AWS: Having trouble serving files with S3

Why go to the trouble of EC2 ...READ MORE

answered Aug 13, 2018 in Cloud Computing by bug_seeker
• 15,360 points