Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

270
Views
Problem in rendering ejs template with Express

I am using Node.js together with Express and EJS.

Below is my code:

var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');

var app = express();
var urlencodedParser = bodyParser.urlencoded({ extended: false });
var publicPath = path.resolve(__dirname, 'public');
app.use(express.static(publicPath));
//app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

app.get('/form_get.html', (req, res) => {
    res.sendFile(__dirname + "/" + "form_get.html")
})

app.get('/process_get', (req, res) => {

    console.log(req.query.first_name);
    res.render(path.join(__dirname+'/views/thankyou.ejs'), { name: req.query.first_name});
    
})

var server = app.listen(3000, () => {
    
    var host = server.address().address;
    var port = server.address().port;
    console.log(`Example app listening at ${host}:${port}`);
})

My folder structure is below:

  • mysql (folder)
    • node_modules (folder)
    • app4.js (file)
    • package.json (file)
    • public (folder, it contains form_get.html)
    • views (folder, it contains thankyou.ejs)

The problem is the failing to look up thankyou.ejs into the views folder.

I get the following error message:

Error: Failed to lookup view "thankyou.ejs" in views directory ".../mysql/views"

What can be the problem?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

When You use app.set('view engine', 'ejs'); It is important to note that res.render() will look in a views folder for the view. In this case


I mirrored Your project and it works fine with couple tiny changes...


Project Folder and file structure.

enter image description here


app.js

var express = require("express");
var path = require("path");
var bodyParser = require("body-parser");

var app = express();
var urlencodedParser = bodyParser.urlencoded({ extended: false });
var publicPath = path.resolve(__dirname, "public");
app.use(express.static(publicPath));
app.set("view engine", "ejs");

app.get("/form_get", (req, res) => {
  res.sendFile(__dirname + "/" + "form_get.html");
});

app.get("/process_get", (req, res) => {
  res.render("thankyou");
});

var server = app.listen(3000, () => {
  var host = server.address().address;
  var port = server.address().port;
  console.log(`Example app listening at ${host}:http://localhost:${port}`);
});

Output:

http://localhost:3000/process_get (thankyou.ejs) file.

enter image description here


Output:

http://localhost:3000/form_get (form_get.html) file

enter image description here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!