Create controller folder in backend and usercontroler.js file create
import User from '../models/usersModel.js'
import asyncHandler from 'express-async-handler'
//getUsers function to get all users
export const getUsers = asyncHandler(async(req, res) => {
const users = await User.find({})
res.json(users)
})
//getUserById function to retrieve user by id
export const getUserById = asyncHandler(async(req, res) => {
const user = await User.findById(req.params.id)
//if user id match param id send user else throw error
if(user){
res.json(user)
}else{
res.status(404).json({message: "User not found"})
res.status(404)
throw new Error('User not found')
}
})
Create route folder and into create routeuser.js
import { getUsers, getUserById } from "../controllers/userController.js";
import express from 'express'
const router = express.Router()
// express router method to create route for getting all users
router.route('/').get(getUsers)
// express router method to create route for getting users by id
router.route('/:id').get(getUserById)
export default router
Install
npm i dotenv --save
Import
import dotenv from 'dotenv'dotenv.config()
Create .env file in root folder
NODE_ENV = development
PORT = 5000
Create server.js in root folder
import connectDB from './backend/config/db.js'
import userRoutes from './backend/routes/userRoute.js'
import express from 'express'
import dotenv from 'dotenv'
//connect database
connectDB()
//dotenv config
dotenv.config()
const app = express()
//Creating API for user
app.use('/api/users', userRoutes)
const PORT = process.env.PORT || 5000
//Express js listen method to run project on http://localhost:5000
app.listen(PORT, console.log(`App is running in ${process.env.NODE_ENV} mode on port ${PORT}`))
Install
npm i nodemon --save-dev
Now add this line under the scripts object inside the package.json file.
"start": "nodemon backend/server.js"
17. Create a react project with the name frontend.
npx create-react-app frontend
18. Install Axios into your react application.
npm install axios --save
19. Now replace all the code from the app.js file with the below-mentioned code.
Before starting our react application add the below line inside the package.json file of the react project or else you will receive a CORS error in your project.
"proxy": "http://127.0.0.1:5000",
20. Now start the application npm start and refresh the browser to see changes.
So now we have successfully built our first MERN project.
React is a JavaScript library for building user interfaces. It is maintained by Facebook and a community of individual developers and companies. React can be used to create single-page applications, mobile applications, and complex user interfaces.
Topics
Topics related to React include:
- Components
- Props
- State
- Hooks
- Virtual DOM
- JSX
- Performance Optimization
- Styling Components
- Testing React Apps
- Accessibility in React Apps
- Server Side Rendering with React.js
All customers get free hands-on experience with popular products, including Compute Engine and Cloud Storage, up to monthly limits.
$300 in free credits for new customers
New customers get $300 in free credits to fully explore and conduct an assessment of Google Cloud. You won’t be charged until you upgrade.
Additional free credits for businesses
New customers who verify their business email will get additional free credits to use while exploring and evaluating Google Cloud.
Free Tier products
There is no charge to use these products up to their specified free usage limit. The free usage limit does not expire, but is subject to change. Available for eligible customers.