Are you a beginner looking to learn more about Next.js and how to build full stack applications using it? If so, you’ve come to the right place! In this tutorial, we will cover everything you need to know about Next.js 14 and how to create a full stack Next.js project in 2024.
Next.js is a popular JavaScript framework that allows you to build dynamic, server-side rendered web applications with ease. It is based on React and offers several features that make building web applications fast and efficient.
To get started with Next.js, you’ll first need to have Node.js installed on your computer. If you don’t already have it installed, you can download it from the official Node.js website. Once you have Node.js installed, you can install Next.js by running the following command in your terminal:
npm install next react react-dom
After installing Next.js, you can create a new Next.js project by running the following command in your terminal:
npx create-next-app my-next-app
This will create a new Next.js project in a directory called my-next-app
. Once the project has been created, you can navigate to the project directory and start the Next.js development server by running the following command:
cd my-next-app
npm run dev
This will start the Next.js development server and you will be able to view your Next.js application in your browser by navigating to http://localhost:3000
.
Next, let’s create a full stack Next.js project. In order to do this, we will need to set up a backend server to handle our API requests. For this tutorial, we will be using Express.js as our backend server.
To install Express.js, you can run the following command in your terminal:
npm install express
Next, create a new file in your project directory called server.js
and add the following code to it:
const express = require('express');
const app = express();
const port = 5000;
app.get('/api', (req, res) => {
res.json({ message: 'Hello from the server!' });
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`)
});
This code sets up a simple Express.js server that listens on port 5000 and returns a JSON response with the message ‘Hello from the server!’ when the /api
endpoint is called.
To make our Next.js application communicate with our backend server, we can use the fetch
API. Update the pages/index.js
file in your Next.js project directory with the following code:
import { useEffect, useState } from 'react';
export default function Home() {
const [message, setMessage] = useState('');
useEffect(() => {
fetch('/api')
.then(res => res.json())
.then(data => setMessage(data.message));
}, []);
return (
<div>
<h1>Welcome to my Full Stack Next.js project!</h1>
<p>Message from the server: {message}</p>
</div>
);
}
This code fetches data from our Express.js server when the component mounts and displays the message returned by the server.
Finally, start your Express.js server by running the following command in your terminal:
node server.js
You can now view your full stack Next.js project by navigating to http://localhost:3000
in your browser. You should see the message ‘Welcome to my Full Stack Next.js project!’ displayed on the page, along with the message ‘Hello from the server!’ received from the backend server.
Congratulations! You have successfully created a full stack Next.js project with a backend server using Express.js. This tutorial covers the basics of using Next.js and building full stack applications with it. Feel free to explore more features of Next.js and experiment with different backend technologies to enhance your project further. Happy coding!
Thank you. Your project help me in my learning journey
Excellent please do some e-commerce applications in next js alteast 3 these will help to the students who are afraid of coding
Nice explaination Love from Hyderabad ❤❤❤❤❤❤❤
If anyone of you have used special characters in your mongoBD password then dont forget to change it into their code in your url you are adding to your file like , the characters # and $ are special characters that should be URL-encoded:
# should be encoded as %23
$ should be encoded as %24
Great Work
Awesome Man!
bro..How can we deploy it? What are the changes to be made for the deployment?
Why did u keep same file name convention ? page.js everywhere ?
made this successfully (i faced issue in mongodb connection and delete button ) but finaly completed
Those who are facing parsing error: Cannot find module 'next/babel'
Goto your .eslintrc.json and copy this :-
{
"extends": ["next/babel","next/core-web-vitals"]
}
great video, great project, clean explanations. thank you very much !
bro mongodb connection is not working
Helpful Thankyou
tqsm
Can you provide the source code repo link?
It was a great Tutorial and the concepts were nicely understood. Recommended for Begineers !
Can U please make videos on next js big projects
Such an incredible video. I made my own putting my mongodburi into a .env file and got it to connect!
Please add authentication and authozation system without any next library
Very easy tutorial to understand, very simple, keep it up brother. and in the future please make another Next.Js tutorial again.
Nice tutorial 👍