how to store and delete s3 image using node js


const s3 = new AWS.S3(
{ accessKeyId: IAM_USER_KEY, /* required */# Put your iam user key
secretAccessKey: IAM_USER_SECRET, /* required */ # Put your iam user secret key
Bucket: BUCKET_NAME /* required */ # Put your bucket name
}
);
s3.deleteObject({
Bucket: MY_BUCKET,
Key: 'some/subfolders/nameofthefile1.extension'
},function (err,data){})

www.codegrepper.com
www.stackoverflow.com

how to send message to specific client using ws node js


var Server = require('ws').Server;
var port = process.env.PORT || 9030;
var ws = new Server({port: port});

var sockets = [];
ws.on('connection', function(w){

var id = w.upgradeReq.headers['sec-websocket-key'];
console.log('New Connection id :: ', id);
w.send(id);
w.on('message', function(msg){
var id = w.upgradeReq.headers['sec-websocket-key'];
var message = JSON.parse(msg);

sockets[message.to].send(message.message);

console.log('Message on :: ', id);
console.log('On message :: ', msg);
});

w.on('close', function() {
var id = w.upgradeReq.headers['sec-websocket-key'];
console.log('Closing :: ', id);
});

sockets[id] = w;
});

www.codegrepper.com
www.stackoverflow.com

how to run multple port node


var http=require('http');
var ports = [7006, 7007, 7008, 7009];
var servers = [];
var s;
function reqHandler(req, res) {
console.log({
remoteAddress: req.socket.remoteAddress,
remotePort: req.socket.remotePort,
localAddress: req.socket.localAddress,
localPort: req.socket.localPort,
});
}
ports.forEach(function(port) {
s = http.createServer(reqHandler);
s.listen(port);
servers.push(s);
});

var http=require('http');
var url = require('url');
var ports = [7006, 7007, 7008, 7009];
var servers = [];
var s;
function reqHandler(req, res) {
var serPort=req.headers.host.split(":");
console.log("PORT:"+serPort[1]);//here i get it using http header.
}
ports.forEach(function(port) {
s = http.createServer(reqHandler);
s.listen(port);
servers.push(s);
});

var express = require('express');

let app1 = express();
let app2 = express();

app1.listen(3000, () => {
console.log("Started server on 3000");
});

app2.listen(3002, () => {
console.log("Started server on 3002");
});

www.codegrepper.com
www.stackoverflow.com

how to sanitize request body in node js


export const registrationSchema = {
    "email": {
        notEmpty: true,
        isEmail: {
            errorMessage: "Invalid Email"
        }
    },
    "password": {
        notEmpty: true,
        isLength: {
            options: [{min: 12}],
            errorMessage: "Must be at least 12 characters"
        },
        matches: {
            options: ["(?=.*[a-zA-Z])(?=.*[0-9]+).*", "g"],
            errorMessage: "Password must be alphanumeric."
        },
        errorMessage: "Invalid password"
    },
    "firstName": {
        notEmpty: false,
        isLength: {
            options: [{max: 200}],
            errorMessage: "The first name must be under 200 characters"
        },
        matches: {
            options: ["^[a-z ,.'-]+$", "i"],
            errorMessage: "The first name can only contain letters and the characters (,.'-)"
        }
    },
    "lastName": {
        notEmpty: false,
        isLength: {
            options: [{max: 200}],
            errorMessage: "The last name must be under 200 characters"
        },
        matches: {
            options: ["^[a-z ,.'-]+$", "i"],
            errorMessage: "The last name can only contain letters and the characters (,.'-)"
        }
    }
};

www.codegrepper.com
www.stackoverflow.com