KEMBAR78
Building Backend-Reach Together | PDF | Computer File | Computing
0% found this document useful (0 votes)
10 views2 pages

Building Backend-Reach Together

This document outlines the steps to generate a base server using Node.js, including setting up a project structure, initializing a package manager, and creating essential files like 'index.js' and '.env'. It details the installation of dependencies such as Express and EJS, as well as the setup of routes and middleware for handling requests and serving static files. Additionally, it briefly mentions functionalities like form submissions, database interactions, and authentication implementations.

Uploaded by

atharvg.cs.23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Building Backend-Reach Together

This document outlines the steps to generate a base server using Node.js, including setting up a project structure, initializing a package manager, and creating essential files like 'index.js' and '.env'. It details the installation of dependencies such as Express and EJS, as well as the setup of routes and middleware for handling requests and serving static files. Additionally, it briefly mentions functionalities like form submissions, database interactions, and authentication implementations.

Uploaded by

atharvg.cs.23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

▦ Generating a Base Server :-

• Setup a Readme.md File (Provides a Description/purpose, Installation


Instructions, Project Structure, Acknowledgements about your project.)
• Open Terminal & initialize your node package manager - 'npm init'
⇝ A 'package.json' file will be created in your Folder.
• Create a folder named - 'Backend'.
• Create entry level file (in Backend Folder) - 'index.js'
• Creating a '.env' file (A simple process that allows you to store environment
variables for your application. These variables can include sensitive information
like API keys or database credentials.)
⇝ Create a '.env' file in Directory.
⇝ In Terminal, type 'npm install dotenv', to install dotenv package.
⇝ In index.js (Backend Folder) - require('dotenv').config(); - to link .env &
index.js files.
⇝ Define Environment Variables: using the KEY=VALUE syntax.
⇝ Now, we can access our environment variables anywhere in your application
using - 'process.env.VARIABLE_NAME'
⇝ Add .env to .gitignore (This ensures that sensitive information stored in
the .env file isn't pushed to your repository.)
• Install Express Dependency (A piece of software that is required for another
piece of software to work correctly.) - 'npm install express'
• Creating our Backend Server:
⇝ Import Express in 'index.js' file.
⇝ Add - "type": "module" - in package.json file (to convert readability into
module format.)
⇝ Install nodemon DevDependency (Packages required only for the development
phase and not for the application to run in production.) using -
'npm install --save-dev nodemon'
⇝ Add - "dev": "nodemon backend/index.js" - to "scripts" (in package.json)
⇝ Create a Base/Home Route in 'index.js' file using app.get & app.listen.
⇝ Run it using Nodemon using - 'npm run dev'.
⇝ Check running of the file at localhost:3000.

▦ Defining Routes:
⇝ Check the working of server.
⇝ Type -
app.use(express.json())
app.use(express.urlencoded({extended: true}));
To simplify the data reaching at our servers. (i.e. We entered a plain
text which upon reaching to server converts to a mesh of many things, so to
simplify it for our server, we use this piece of code.)

⇝ Working up with '.ejs' file.


⇝ Install using 'npm i ejs'.
⇝ Set up ejs as a view engine.
app.set('view engine', 'ejs');
app.set('views', path.join('views'));
⇝ Set up a folder 'views'.

⇝ Setting up public static file (Setting up CSS, Vanilla JavaScript files).


⇝ Install path file using - 'npm i path'
⇝ Import path file using - import path from 'path'
⇝ Type -
app.use(express.static(path.join('public'))); - [Middleware]
⇝ Create a folder named 'public' at the root level.
⇝ Import css files to 'public' folder.
⇝ Run it on your server.

⇝ Base Routing :
⇝ Replace .send with .render.

⇝ We can use res.render to place value of a specific variable (Adding Dynamic


Content to .ejs file):-
⇝ res.render('fileName', {varName: varValue}) - [in index.js file]
⇝ <h1> Welcome to <%= varName %></h1>

⇝ Dynamic Routing :


⇝ Create a sub-folder 'Routes' (inside Backend), & create a file 'routes.js'
⇝ Export created routes from 'routes.js' & import it in 'index.js'.

▦ Adding Functionalities:
⇨ Handling Form Submissions :




⇨ Interacting with Databases :


⇨ Implementing Authentications/APIs:

You might also like