0% found this document useful (0 votes)
4 views4 pages

MERN Simple Notes

The document provides an overview of ExpressJS, MongoDB, ReactJS, and Node.js, covering key concepts such as routing, middleware, sessions, cookies, REST APIs, and CRUD operations. It includes syntax examples for various functionalities like handling requests, managing user sessions, and performing database operations. Additionally, it touches on ReactJS components, props, state, and advanced features like conditional rendering and hooks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

MERN Simple Notes

The document provides an overview of ExpressJS, MongoDB, ReactJS, and Node.js, covering key concepts such as routing, middleware, sessions, cookies, REST APIs, and CRUD operations. It includes syntax examples for various functionalities like handling requests, managing user sessions, and performing database operations. Additionally, it touches on ReactJS components, props, state, and advanced features like conditional rendering and hooks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

ExpressJS Basics
a) Routing

 Definition: Routing decides how an application’s endpoint (URL) responds to client


requests.
 Syntax:

app.get('/path', (req, res) => { ... });

 Example:

app.get('/', (req, res) => res.send('Hello World'));

b) Middleware

 Definition: Functions that run between request and response cycle.


 Syntax:

app.use((req, res, next) => { ...; next(); });

 Example:

app.use((req, res, next) => {


console.log('Request received');
next();
});

c) HTTP Methods

 Definition: Used to perform CRUD (Create, Read, Update, Delete).


 Syntax:

app.post(), app.get(), app.put(), app.delete()

 Example:

app.post('/add', (req, res) => res.send('Data added'));

2. ExpressJS – Sessions & Authentication


a) Cookies

 Definition: Small data stored on client side.


 Syntax:

res.cookie('name', 'value');

 Example:
res.cookie('user', 'John');

b) Sessions

 Definition: Store user data across requests.


 Syntax:

req.session.key = value;

 Example:

req.session.user = 'John';

3. ExpressJS – REST API


 Definition: API that follows HTTP methods for CRUD.
 Syntax:

app.get('/api/items', (req, res) => {...});

 Example:

app.get('/users', (req, res) => res.json([{id:1,name:'A'}]));

4. MongoDB (Mongoose)
a) Connection

 Definition: Connects Node.js to MongoDB.


 Syntax:

mongoose.connect('mongodb://localhost/db');

 Example:

mongoose.connect('mongodb://localhost/test');

b) CRUD Operations

 Insert:

User.create({name:"John"});

 Find:

User.find({});

 Update:
User.updateOne({name:"John"},{name:"Mike"});

 Delete:

User.deleteOne({name:"John"});

5. ReactJS Basics
a) JSX

 Definition: JavaScript + HTML syntax in React.


 Syntax:

const el = <h1>Hello</h1>;

 Example:

function App(){ return <h1>Hello World</h1>; }

b) Components

 Function Component:

function Hello(){ return <p>Hello</p>; }

 Class Component:

class Hello extends React.Component {


render(){ return <p>Hello</p>; }
}

c) Props & State

 Props (data from parent):

function Welcome(props){ return <h1>{props.name}</h1>; }

 State (internal data):

const [count, setCount] = useState(0);

6. ReactJS Advanced
a) Conditional Rendering
{ isLoggedIn ? <p>Welcome</p> : <p>Please login</p> }

b) Lists
{items.map(i => <li key={i}>{i}</li>)}

c) Forms
<input type="text" value={name} onChange={e => setName(e.target.value)} />

d) Router
<Route path="/home" element={<Home />} />

e) Hooks
useEffect(() => { console.log("Mounted"); }, []);

7. Node.js Core
a) File System
const fs = require('fs');
fs.writeFileSync('test.txt', 'Hello');

b) Events
const EventEmitter = require('events');
const emitter = new EventEmitter();
emitter.on('event', () => console.log('Event Fired'));
emitter.emit('event');

You might also like