NPM Package Installation

NPM Package Installation Explained

NPM Package Installation Explained

To install the necessary packages for your Node.js project, use the following command:

npm i cors @withvoid/make-validation express jsonwebtoken mongoose morgan socket.io uuid --save

cors

cors is a package for enabling Cross-Origin Resource Sharing (CORS) in your Express application.

const cors = require('cors');
const app = express();
app.use(cors());

@withvoid/make-validation

@withvoid/make-validation is a simple and customizable validation library for Node.js.

const { makeValidation } = require('@withvoid/make-validation');
const validation = makeValidation(types => ({
    name: types.string.required,
    age: types.number
}));
const result = validation.validate({ name: 'John', age: 30 });

express

express is a fast and minimalist web framework for Node.js.

const express = require('express');
const app = express();
app.get('/', (req, res) => res.send('Hello World!'));
app.listen(3000, () => console.log('Server running on port 3000'));

jsonwebtoken

jsonwebtoken is used to create and verify JSON Web Tokens (JWT) for authentication.

const jwt = require('jsonwebtoken');
const token = jwt.sign({ userId: 123 }, 'your-256-bit-secret', { expiresIn: '1h' });
jwt.verify(token, 'your-256-bit-secret', (err, decoded) => {
    if (err) {
        console.error(err);
    } else {
        console.log(decoded);
    }
});

mongoose

mongoose is an object data modeling (ODM) library for MongoDB and Node.js.

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test', { useNewUrlParser: true, useUnifiedTopology: true });
const Cat = mongoose.model('Cat', { name: String });
const kitty = new Cat({ name: 'Zildjian' });
kitty.save().then(() => console.log('meow'));

morgan

morgan is a HTTP request logger middleware for Node.js.

const morgan = require('morgan');
const app = express();
app.use(morgan('combined'));

socket.io

socket.io is a library that enables real-time, bidirectional and event-based communication between web clients and servers.

const socketIo = require('socket.io');
const io = socketIo(server);
io.on('connection', socket => {
    console.log('a user connected');
    socket.on('disconnect', () => console.log('user disconnected'));
});

uuid

uuid is a package for generating universally unique identifiers (UUIDs).

const { v4: uuidv4 } = require('uuid');
console.log(uuidv4());

Comments

Popular posts from this blog

Tech Duos For Web Development

CIFAR-10 Dataset Classification Using Convolutional Neural Networks (CNNs) With PyTorch

Long-short-term-memory (LSTM) Word Prediction With PyTorch