Building a fullstack React.js application with Node.js, Express.js & MongoDB (MERN)
- MongoDB - document database
- Express(.js) - Node.js web framework
- React(.js) - a client-side JavaScript framework
- Node(.js) - the premier JavaScript web server
npm init --prefix backend
npm install --save express body-parser --prefix backend
npm install --save-dev nodemon --prefix backend
npm start --prefix backend
npm install --save uuid --prefix backend
npm install --save express-validator --prefix backend
npm install --save axios --prefix backend
npm install dotenv --save --prefix backend
npm install --save mongoose --prefix backend
npm install --save mongoose-unique-validator --prefix backend
npm install --save multer --prefix backend
npm install --save bcryptjs --prefix backend
npm install --save jsonwebtoken --prefix backend
npm start --prefix backend
npm start --prefix frontend
(don't forget to change package.json from"start": "node app.js"
to"start": "nodemon app.js"
)
- Postman used for Node.js API testing
- Test frontend locally -> Simple local server ->
npm install -g serve --prefix frontend
(can serve static site) cd frontend/build/
serve
- JWT uses HMAC alorifthm by default (HS256)
- Involves a combination of a hashing function and one (secret) key
- Shared between the two parties used to generate the hash that will serve as the signature
- Great care must be taken to protecting the key, becuase the same key is used to generate the signture and to perform validation
- Cross-site scripting (XSS) vunerability, this is vunerable as the local storage used to persist the tokens is accessible to all JS on the same domain
- To note: React does have some protective measures in place for XSS
- Used dotenv to store environment variables for frontend
- Backend is using Heroku to source configured environment variables
npm run build
-> Optimises, provides only essentials (strips out debugging)- New build folder will be created with all files needed to deploy
- Nodemon is used as tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory detected. (not to use in production environment)
- Create heroku app and use git to push changes 😀
sudo npm install -g firebase-tools --prefix frontend
firbase login
firebase init
configuration below:
Which Firebase features do you want to set up for this directory? Press Space to select features, then Enter to confirm your choices
->Hosting: Configure files for Firebase Hosting and (optionally) set up GitHub Action deploys
Please select an option:
->Create a new project
What do you want to use as your public directory?
->build
? Configure as a single-page app (rewrite all urls to /index.html)?
->Yes
firebase deploy
npm run build --prefix frontend
- Create
public
folder in backend directory - Copy
frontend/build
contents tobackend/public
git add .
git commit -m "added react frontend"
git push heroku master
- Code splitting is used to "lazy-load" to create code chunks as and when a user needs a certain component, this increases performance.
Usage of Suspense -> import React, { Suspense } from "react";
Lazy load component -> const Users = React.lazy(() => import("./user/pages/Users"));
Suspense is used to handle loading of components:
return (
<AuthContext.Provider
value={{
isLoggedIn: !!token,
token: token,
userId: userId,
login: login,
logout: logout,
}}
>
<Router>
<MainNavigation />
<main>
<Suspense
fallback={
<div className="center">
<LoadingSpinner />
</div>
}
>
{routes}
</Suspense>
</main>
</Router>
</AuthContext.Provider>
);
};