How To Get Data From MySQL Table to EJS page using NodeJS

[UPDATE: There is an error on loading of images in every post. I am working to resolve this as soon as possible. Screenshots were mere snap of windows so you can still follow the steps written in text and do the work!] 

Welcome to another tech post. In this post, I will be showing you how to get data from the MySQL table which is made using SequelizeJS into our view which is an EJS file in NodeJS.

If you want to learn how to create a table using SequelizeJS, then refer to my other post: How To Create MySQL Table with SequelizeJS (sumitkp.in)


Before starting to read further, make sure you have prior knowledge of NPM packages, NodeJS, SequelizeJS, EJS, JSON and ExpressJs.


Something about the EJS:

  • EJS stands for Embedded JavaScript.
  • EJS is a simple templating language that lets us generate HTML markup with plain JavaScript.
  • It offers fast development time.
  • It has a simple syntax and speedy execution.
  • It is easy to debug EJS errors.
  • EJS has a large community of active users and the library is under active development.
  • Refer to EJS here: EJS -- Embedded JavaScript templates

1. Create a server.js file in the root directory of your project.


Refer to this code snippet to learn how to get all records from the table and convert it into JSON: SequelizeJS - findAll( ) method

2. Create an EJS file under 'views' in the root directory of your project.
To display, use <%-   %>. To learn more about such tags, click here.

This is how our JSON Data is:




After we start the application by typing 'node server.app' in the terminal and go to the URL http://localhost:3000/student', this is what is displayed:



The following code is of index.js and student_models.js files under './models' folder.



The following code is of db_config.js and it is under './config' folder.

Comments

Popular

How To Create MySQL Table with SequelizeJS

How To Read CSV Files in Java as Maven Project

How To Create A Hibernate Project in Maven