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 17, 2019 in Others by shubham
• 7,340 points
3,810 views

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) {
	res.render('index');					  
});

app.listen(3000);
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 17, 2019 by sunshine
• 1,300 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, 2019 in Others by DataKing99
• 8,240 points
853 views
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, 2019 in Others by Nabarupa
401 views
+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 4, 2019 in Others by Priyanka
452 views
0 votes
1 answer

How to convert data from txt files to Excel files using python

Hi , there are few steps to ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,670 points
13,367 views
0 votes
1 answer

How to open .xlsx files in MS Excel from VS Code?

Hello, to open xlxs files, or files ...READ MORE

answered Feb 17, 2022 in Others by gaurav
• 23,260 points
3,095 views
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,740 points
711 views
0 votes
1 answer
0 votes
0 answers

Anyone can help me out to understand the semantic of (document.getElementBYId("demo").innerHTML="Hello") ?

Hello guys, Can Someone helps me to find ...READ MORE

Jan 17, 2020 in Web Development by anonymous
• 37,510 points
748 views
+1 vote
1 answer

What is the relationship between angularjs Scope with controller/view?

Let us consider the below block: <div ng-controller="emp"> ...READ MORE

answered Jan 20, 2020 in Web Development by Niroj
• 82,880 points

edited Jan 21, 2020 by Niroj 803 views
+1 vote
1 answer

Simple Unit Testing NodeJS/Express

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

answered Jul 16, 2019 in Others by sunshine
• 1,300 points
983 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP