NPM Package Installation
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());
More info: cors on npm
@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 });
More info: make-validation on npm
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'));
More info: express on npm
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);
}
});
More info: jsonwebtoken on npm
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'));
More info: mongoose on npm
morgan
morgan is a HTTP request logger middleware for Node.js.
const morgan = require('morgan');
const app = express();
app.use(morgan('combined'));
More info: morgan on npm
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'));
});
More info: socket.io on npm
uuid
uuid is a package for generating universally unique identifiers (UUIDs).
const { v4: uuidv4 } = require('uuid');
console.log(uuidv4());
More info: uuid on npm
Comments