Lab File Format PDF
Lab File Format PDF
Course Name:
Course Code:
Galgotias University
Plot No. 2, Sector-17A, Yamuna Expressway, Greater Noida, Gautam Buddha Nagar, Uttar
Pradesh, India Contact no. 0120–4370000,
Index
S.NO CONTENTS SIGNATURE
Building a simple web application using Node.js, Express and React that
3 implements user authentication and authorization.
Building a web application using Node.js and React that consumes data
4 from a REST API.
Building a web application using Node.js, React and Redux for state
7 management.
Building a web application using Node.js, React and Jest for unit testing.
8
Building a web application using Node.js and React that consumes data
11 from a REST API.
Building a web application using Node.js, React and Webpack for asset
12 management and optimization.
Building a web application using Node.js and React that uses the latest
13 features of ECMAScript and Label.
LAB-1
write a program to create CRUD operation using php with mysql database
// Database connection parameters
$host = 'localhost';
$username = 'your_username';
$password = 'your_password';
$database = 'your_database';
// CREATE operation
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Retrieve data from the form
$name = $_POST['name'];
$email = $_POST['email'];
// READ operation
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// Display the data
while ($row = mysqli_fetch_assoc($result)) {
echo "ID: " . $row['id'] . "<br>";
echo "Name: " . $row['name'] . "<br>";
echo "Email: " . $row['email'] . "<br><br>";
}
} else {
echo "No records found.";
}
// UPDATE operation
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update'])) {
$id = $_POST['id'];
$newName = $_POST['newName'];
$newEmail = $_POST['newEmail'];
// DELETE operation
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete'])) {
$id = $_POST['id'];
?>
<label for="email">Email:</label>
<input type="email" name="email" id="email" required><br>
<button type="submit">Create</button>
</form>
Building a simple CRUD (Create, Read, Update, Delete) web application using Node.js
and a SQL database.
Certainly! Here's an example of a simple CRUD web application using Node.js and a SQL database (MySQL):
db.connect((err) => {
if (err) throw err;
console.log('Connected to MySQL database');
});
```
Building a simple web application using Node.js, Express and React that implements user
authentication and authorization
Sure! Here's a high-level overview of building a simple web application using Node.js, Express, and React that implements user
authentication and authorization:
4. Authentication Flow:
- User Registration:
- Display a signup form where users can enter their desired credentials (e.g., username, email, password).
- On form submission, send a POST request to the backend server to create a new user account.
- Store the user's JWT token in the browser's local storage or cookies.
- User Login:
- Display a login form where users can enter their credentials (e.g., username, email, password).
- On form submission, send a POST request to the backend server to validate the user's credentials.
- If the credentials are valid, store the user's JWT token in the browser's local storage or cookies.
- Protected Routes:
- Define routes that require authentication on the backend server.
- On the frontend client, implement protected routes using React Router.
- Check for the presence of a valid JWT token in the browser's local storage or cookies before accessing protected routes.
- If the token is invalid or missing, redirect the user to the login page.
- Logout:
- Provide a logout button or link to allow users to log out.
- On logout, remove the JWT token from the browser's local storage or cookies.
Building a web application using Node.js and React that consumes data from a REST API.
Certainly! Here's a high-level overview of building a web application using Node.js and React that consumes data from a REST API:
Remember to handle any necessary error handling, loading states, and authentication/authorization if required by the REST API.
LAB-5
Building a web application using Node.js, React, and a NoSQL database that implements server-side
rendering.
Certainly! Here's a high-level overview of building a web application using Node.js, React, and a NoSQL database that implements
server-side rendering:
5. Server-side Rendering:
- Configure your backend server to render the initial HTML on the server.
- Set up a template engine (e.g., EJS, Pug) to generate the HTML templates and inject dynamic data.
- Define a server route that handles the request for the initial HTML and renders the template with the fetched data.
- Use the `renderToString` method from your chosen template engine or a server-side rendering framework (e.g., Next.js) to
generate the initial HTML.
6. Test and Deployment:
- Test your application thoroughly, including server-side rendering functionality, data retrieval from the NoSQL database, and
client-side interactivity.
- Consider writing unit tests for your backend API routes and integration tests for the server-side rendering functionality.
- Deploy your backend server and frontend client separately or together on a hosting platform of your choice (e.g., Heroku, AWS,
Netlify).
- Configure your deployment to ensure proper server-side rendering functionality.
Remember to handle any necessary error handling, loading states, and authentication/authorization if required by your
application.
LAB-7
Building a web application using Node.js, React and Webpack for asset management and
optimization.
Certainly! Here's an example of building a web application using Node.js, React, and Webpack for asset management and
optimization:
3. Configure Webpack:
- Create a `webpack.config.js` file in the root directory of your project.
- Configure Webpack with the necessary loaders and plugins:
```javascript
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react'],
},
},
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
}),
],
devServer: {
contentBase: path.resolve(__dirname, 'dist'),
hot: true,
port: 3000,
},
};
```
4. Configure Babel:
- Create a `.babelrc` file in the root directory of your project.
- Configure Babel to use the necessary presets:
```json
{
"presets": ["@babel/preset-env", "@babel/preset-react"]
}
```
function App() {
return <h1>Hello, World!</h1>;
}
Building a web application using Node.js, React and Redux for state management.
Certainly! Here's an example of building a web application using Node.js, React, and Redux for state management:
3. Set up Redux:
- Open `reducers.js` and add the following code:
```javascript
// reducers.js
import { combineReducers } from 'redux';
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);
```
function App() {
const counter = useSelector((state) => state.counter);
const dispatch = useDispatch();
return (
<div>
<h1>Counter: {counter}</h1>
<button onClick={handleIncrement}>Increment</button>
<button onClick={handleDecrement}>Decrement</button>
</div>
);
}
- Open your browser and navigate to `http://localhost:3000` to see the application running.
LAB-9
Building a web application using Node.js, React and Jest for unit testing
Certainly! Here's an example of building a web application using Node.js, React, and Jest for unit testing:
function App() {
return <h1>Hello, World!</h1>;
}
- Jest will execute the tests and provide the test results.
In this example, we used Jest and `react-test-renderer` to write a unit test for the `App` component. The test renders the
component and compares the rendered output with a snapshot, ensuring that the component is rendered correctly. You can add
more tests for other components or functionalities of your web application in the respective test files.
Lab-10
Building a web application using Node.js and React that uses websockets for real-time
communication between the client and the server.
Certainly! Here's an example of building a web application using Node.js and React that uses websockets for real-time
communication between the client and the server:
socket.on('disconnect', () => {
console.log('A client disconnected');
});
});
ReactDOM.render(
<App socket={socket} />,
document.getElementById('root')
);
```
- Open `App.js` inside the `client` directory and add the following code:
```javascript
import React, { useState, useEffect } from 'react';
useEffect(() => {
socket.on('message', (data) => {
setReceivedMessage(data);
});
}, [socket]);
return (
<div>
<input
type="text"
value={message}
onChange={(e) => setMessage(e.target.value)}
/>
<button onClick={sendMessage}>Send</button>
<p>Received message: {receivedMessage}</p>
</div>
);
}
- You can open multiple browser tabs/windows to test the real-time communication by entering messages in one client and
seeing them appear in all connected clients.
In this example, we used the `socket.io` library to enable real-time communication between the client and the server. The server
listens for socket connections and handles events such as receiving and broadcasting messages. The client establishes a socket
connection to the server and sends
Lab-11
Building a web application using Node.js, React, and a NoSQL database that implements server-side
rendering
Certainly! Here's an example of building a web application using Node.js, React, and a NoSQL database (MongoDB) that
implements server-side rendering:
// Connect to MongoDB
mongoose.connect('mongodb://localhost/my_database', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
- Open `App.js` inside the `client` directory and add the following code:
```javascript
import React from 'react';
Building a web application using Node.js and React that consumes data from a REST API.
Certainly! Here's an example of building a web application using Node.js and React that consumes data from a REST API:
- Open `App.js` inside the `client` directory and add the following code:
```javascript
import React, { useEffect, useState } from 'react';
import axios from 'axios';
function App() {
const [data, setData] = useState([]);
useEffect(() => {
// Fetch data from the server
const fetchData = async () => {
try {
const response = await axios.get('/api/data');
setData(response.data);
} catch (error) {
console.error(error);
}
};
fetchData();
}, []);
return (
<div>
<h1>Data from REST API:</h1>
<ul>
{data.map((item) => (
<li key={item.id}>{item.name}</li>
))}
</ul>
</div>
);
}
- Open a browser and navigate to `http://localhost:3000` to see the web application consuming data from the REST API.
In this example, the backend server defines a route `/api/data` that fetches data from a REST API using the `axios` library. The
frontend client, implemented with React, makes a request to the backend server to retrieve the data and renders it in a list
format.
Lab-13
Building a web application using Node.js, React and Webpack for asset management and optimization
Sure! I can provide you with a basic setup for building a web application using Node.js, React, and Webpack. This setup will help
you with asset management and optimization. Here are the steps:
1. Create a file named `webpack.config.js` in the root of your project directory and add the following configuration:
```javascript
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react'],
},
},
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
}),
],
devServer: {
contentBase: path.join(__dirname, 'dist'),
compress: true,
port: 3000,
},
};
```
1. Create a new directory named `src` in the root of your project directory:
```
mkdir src
```
2. Create a file named `index.js` inside the `src` directory and add the following code:
```javascript
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
3. Create a file named `App.js` inside the `src` directory and add a simple React component:
```javascript
import React from 'react';
4. Create a file named `index.html` in the root of your project directory and add the following code:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My App</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
```
1. Open the `package.json` file and add the following scripts under the `"scripts"` section:
```json
"scripts": {
"start": "webpack serve --mode development --open",
"build": "webpack --mode production"
}
```
This will start the development server on `http://localhost:3000` and automatically open it in your default browser. You should
see "Hello, World!" displayed on the page.
Building a web application using Node.js and React that uses the latest features of ECMAScript and
Label with code
Certainly! To build a web application using Node.js and React with the latest features of ECMAScript and Babel, follow these
steps:
1. Create a file named `.babelrc` in the root of your project directory and add the following configuration:
```json
{
"presets": [
["@babel/preset-env", {
"targets": {
"node": "current"
}
}],
"@babel/preset-react"
]
}
```
1. Create a file named `webpack.config.js` in the root of your project directory and add the following configuration:
```javascript
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
},
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
}),
],
devServer: {
contentBase: path.join(__dirname, 'dist'),
compress: true,
port: 3000,
},
};
```
1. Create a new directory named `src` in the root of your project directory:
```
mkdir src
```
2. Create a file named `index.js` inside the `src` directory and add the following code:
```javascript
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
3. Create a file named `App.js` inside the `src` directory and add a simple React component using the latest ECMAScript features:
```javascript
import React, { useState } from 'react';
return (
<div>
<h1>Hello, World!</h1>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
</div>
);
};
4. Create a file named `index.html` in the root of your project directory and add the following code:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My App</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
```
1. Open the `package.json` file and add the following scripts under the `"scripts"` section:
```json
"scripts": {
"start": "webpack serve --mode development