Databases-NodeJS-Demo
Demo created with other Languages and Technologies
Dependencies
Required NodeJS Dependencies
- express - minimal and flexible Node.js web application framework that provides a robust set of features to develop web and mobile applications
- mysql2 - mysql driver
- ejs - a templating engine
- faker - to create dummy data
- express-session - Create session middleware
- connect-flash - The flash is a special area of the session used for storing messages
Optional NodeJS Dependencies
- nodemon - a command-line interface (CLI) utility developed by @rem that wraps your Node app, watches the file system, and automatically restarts the process
- chalk - style terminal strings
- custom-env - configure different environments for your project. For example you can create a file named
.env.localhost
and keep the variables when you're in the development phase. Usually used so as to keep credentials safe, so as they don't get uploaded to any hosting platforms that are used for Version Control like GitHub
custom-env Example
- Create a file named .env.localhost
- Import it by using
require('custom-env').env('localhost');
.env.localhost
content
SERVER_PORT=3000
DB_HOST=localhost
DB_PORT=3306
DB_USER=dbuser
DB_PASS=dbpass
DB=db-name
Useful Tools
- Database Administration Tools like Dbeaver, Navicat, MySQL Workbench
Project Folder Hierarchy
- controllers - the logic of the routes (where the sql queries take place, and then the result can be either returned rendered or in other ways like JSON, csv etc.
- public - all publicly accessible files that a user of the system can have access (usually css, icons, static pages etc)
- routes - the endpoints of the application. Endpoints are simply a unique URL where a specific task is completed. For example http://localhost:3000/signin is used for signing in an application, http://localhost:3000/user/5 is to get the user with the id eq to 5 etc
- utils - can contain any utilities you want for your project, for example the connection of the database, so as you can just import and use it when needed (mostly in controllers)
- views - the pages of the project (More on what the templating engine does)
What is a Templating Engine
A template engine enables you to use static template files in your application. At runtime, the template engine replaces variables in a template file with actual values, and transforms the template into an HTML file sent to the client. This approach makes it easier to design an HTML page.
Alternatives of EJS
NodeJS General Information
- To initialize a new NodeJS Project, create the project folder, open a terminal and use the command
npm init
- To install a new package use
npm i [package-name]
. If you want to save the package in DevDependencies usenpm i [package-name] --save-dev
- To uninstall a package use
npm u [package-name]
- To add a new script open
package.json
, find scripts and below a script can be added. Usually astart script
is added.
Example:"start": "nodemon server.js"
, ifnodemon
is used in the project.
To use the script executenpm start
Create Dummy Data
- Using the faker package dummy data can be created. It can create all kind of data (More on the documentation of the package)
- To run the
creator.js
script which is located insidedummyDataCreator
folder, runnpm run create-data
, which is a custom script (custom scripts can be declared inpackage.json
and can be run withnpm run <script-name>
)