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, 2019 in Others by shubham
• 7,320 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, 2019 by sunshine
• 1,280 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
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
+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, 2019 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,700 points
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
+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,720 points

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

Simple Unit Testing NodeJS/Express

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

answered Jul 15, 2019 in Others by sunshine
• 1,280 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,550 points