Node: send response after all work is done - node.js

If I call "http://localhost:3000/?fqn=mfgthun.ch&port=80" then i get back the following string:
{"source":"U259636","destination":"mfgthun.ch","ips":[]}
The resoult should be the following:
{"source":"U259636","destination":"mfgthun.ch","ips":[{"ip":"87.237.169.85","reachable":false}]}
The part in the function Write is missing... Do i need to add another callback here? Or is there any other solution for that?
Why is it not waiting until the first app.get block has finished?
const express = require('express')
const app = express()
const isReachable = require('is-reachable');
var dns = require('dns');
var os = require('os');
var url = require('url');
var jsonResponse;
function Write (ipPort, ip) {
this.ipPort = ipPort;
this.ip = ip;
this.callback = function(reachable) {
var temp = {ip: ip, reachable: reachable };
global.jsonResponse.ips.push(temp);
};
}
app.get("/", function(httpRequest, httpResponse, next){
try {
var url_parts = url.parse(httpRequest.url, true);
var query = url_parts.query;
global.jsonResponse = { source: os.hostname(), destination: query.fqn, ips: [] };
dns.resolve4(query.fqn, function (err, addresses) {
if (err) throw err;
for(var i = 0; i < addresses.length; i++) {
var ipPort = addresses[i] + ':' + query.port;
var write = new Write(ipPort, addresses[i]);
isReachable(ipPort).then(write.callback);
};
});
} catch(err) {
console.log(err);
};
next();
});
app.get("/", function(httpRequest, httpResponse){
httpResponse.write(JSON.stringify(global.jsonResponse));
httpResponse.end();
});
app.listen(3000)

Related

How to get data from another route (POST method)?

I have multiple routes setup. I want to get specific data from another route. That data is coming from a post method.
My server.js look like this:
var mysql = require('mysql')
var morgan = require('morgan')
var cors = require('cors')
var bodyParser = require('body-parser')
var http = require('http')
var dateFormat = require('dateformat')
const port = process.env.PORT || 3000;
//middleware
var app = express()
app.use(cors())
app.use(morgan('dev'))
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}))
var now = new Date()
//routes
var user_details = require('./routes/users')
var user_orders = require('./routes/order')
//Use routes
app.use('/', user_details)
app.use('/', user_orders)
//Launch Server
app.listen(port, () => {
console.log('Server start at port: ' + port)
})
My routes/users.js :
var router = express.Router()
var db = require('../dbConfig')
var randomstring = require("randomstring");
var moment = require('moment')
router.post('/list', (req, res) => {
var appendRandomString = randomstring.generate({
length: 10,
capitalization: 'uppercase',
readable: true
})
var id = 'PEPPR_' + appendRandomString
var email = req.body.email
var listItems = req.body.listItems
var listTitle = req.body.listTitle
var date = moment().format("Do MMMM YYYY");
var time = moment().format("LT");
const INSERT_USER_LISTS = `INSERT INTO user_lists (id,date,time,user_email,list_title,list_items) VALUES('${id}','${date}','${time}','${email}','${listTitle}','${listItems}')`
db.query(INSERT_USER_LISTS, (err, success) => {
if (err) {
return res.send(err)
} else {
console.log('list added')
res.send('list added')
}
})
})
module.exports = router
And my routes/order.js
var router = express.Router()
var db = require('../dbConfig')
var randomstring = require("randomstring");
var moment = require('moment')
var user_details = require('./users')
router.post('/sendOrder', (req, res) => {
var email = req.body.email
var status = 'Order Confirmed'
var date = moment().format("Do MMMM YYYY");
var time = moment().format("LT");
var appendRandomString = randomstring.generate({
length: 10,
capitalization: 'uppercase',
readable: true
})
var id = 'PEPPR_ORDER_' + appendRandomString
var list_items = ''
var list_title = ''
var data = {
id: id,
email: email,
list_items: list_items,
list_title: list_title,
date: date,
time: time,
status: status
}
const CREATE_ORDER = `INSERT INTO user_orders SET ?`
db.query(CREATE_ORDER, data, (err, success) => {
if (err) {
return res.send(err)
} else {
res.send('oc')
}
})
})
module.exports = router
I want the list_items and list_title in my order.js from users.js , this two data is coming from a POST method as you can see in users.js
If I understood you correctly you want to use request body which comes to /users in another route /orders.
You are saving user data list_title and list_item in users table. So all you need to access the data from /orders route is make an additional query to db where you will select users by id\email.
Not sure which db ORM you use but in general cases your code may look like this:
router.post('/sendOrder', async (req, res) => {
const { email } = req.body;
// declare other fields
const user = await db.query(
`SELECT * FROM users WHERE email LIKE '%${email}%'`,
(err, success) => {
if (err) {
return res.send(err);
} else {
res.send('ok');
}
}
);
// declare `data` object with user.list_title, user.list_item
const CREATE_ORDER = `INSERT INTO user_orders SET ?`;
db.query(CREATE_ORDER, data, (err, success) => {
if (err) {
return res.send(err);
} else {
res.send('ok');
}
});
});

React Node app crashing while downloading file

I am creating an app using Node and react as part of my learning process. The which covert HTTP, https links to torrent. The problem is, I need to download the file first to convert it to torrent. The app working fine when it comes to small files. But if it is a large file like One GB it fails and app crash.
here is my project link:-
https://github.com/savadks95/FireBit/blob/master/server.js
Can anyone please tell me how to fix this. And I also want to show the download in progress.
var http = require("http");
var webtorrentify = require("webtorrentify-link");
var fs = require("fs");
var url = require("url");
var path = require("path");
var validUrl = require("valid-url");
var express = require("express");
var getUrls = require("get-urls");
var remote = require("remote-file-size");
var app = express();
var downloadLink;
var fileName;
var fileSize;
var server;
var parsed;
var param;
var link;
var port;
port = process.env.PORT || 80;
app.get("/favicon.ico", function(req, res) {
console.log("favicon request recived");
});
app.get("*", function(req, res) {
if (req.url === "/") {
//app.use('/public/html', express.static(path.join(__dirname)));
fs.readFile("public/html/index.html", function(err, data) {
res.write(data);
});
} else if (req.url === "/l?thelink=") {
fs.readFile("public/html/emptyRequest.html", function(err, data) {
res.write(data);
res.end();
});
} else {
//---------Reciving Url--------------------
console.log(req.query.thelink);
downloadLink = req.query.thelink;
//-----------------------------------------
//------------checking for valid url-------
if (validUrl.isUri(downloadLink)) {
console.log("Looks like an URI");
//-----------------------------------------
//----------Extracting filename-------------
parsed = url.parse(downloadLink);
fileName = path.basename(parsed.pathname);
console.log(path.basename(parsed.pathname));
//-------------------------------------------
//----------Finding File size----------------
remote(downloadLink, function(err, o) {
fileSize = o / 1024 / 1024;
console.log("size of " + fileName + " = " + fileSize + " MB");
//-------------------------------------------
if (fileSize < 501) {
///////////////Creating Torrent////////////////////
webtorrentify(downloadLink).then(function(buffer) {
console.log("creating the torrent");
//res.send('what is');
//-------------------------------------------
res.setHeader("Content-Type", "application/x-bittorrent");
res.setHeader(
"Content-Disposition",
`inline; filename="${fileName}.torrent"`
);
res.setHeader("Cache-Control", "public, max-age=2592000"); // 30 days
res.send(buffer);
console.log(fileName + ".torrent created");
res.end();
//-------------------------------------------
});
////////////////////////////////////////////////
} else {
console.log("More than 500 MB");
res.send("<h4> More than 500 MB or invalid URL </h4>");
}
});
} else {
console.log("not url");
fs.readFile("public/html/404.html", function(err, data) {
res.write(data);
res.end();
});
}
}
});
app.listen(port);

What is the best way to pass information from app.js?

In the code below, what is best way to pass information from app.js to choices.js ?
Currently, I put them all in a variable called answer.
Is that the best way to do it ?
Thank you.
These are the code in app.js:
var express = require("express");
var cors = require("cors");
var bodyParser = require("body-parser");
var app = express();
var sql = require('msnodesqlv8');
:
var choicesTerm = [];
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(function (req, res, next) {
console.log(`${req.method} request for '${req.url}' -
${JSON.stringify(req.body)}`);
next();
});
var connStr = "Driver={SQL Server Native Client 11.0};....";
sql.open(connStr, function (err, conn) {
var pm = conn.procedureMgr();
pm.callproc('mySP',sessionID, function (err, results) {
:
pm.callproc('mySp2', function (err, results) {
:
if (results.length)
{
var row = results[0];
var Answers = [];
Answers = row.Answers.split("|");
for (var i = 0, len = Answers.length; i < len; i++) {
var answer = {value: Answers[i], description: Answers[i], question: row.text, detail: row.detail}; //--> pass information to choices.js
choicesTerm[i] = answer;
}
}
})
});
});
app.use(express.static("./public"));
app.use(cors());
app.get("/choices-api", function(req, res) {
res.json(choicesTerm);
});
These are the code in choices.js:
$(document).ready(function () {
$.getJSON('/choices-api', printTerms);
});
function printTerms(terms) {
var question = terms[0].question; //--> This is passed from app.js
var detail = terms[0].detail; //--> This is passed from app.js
}

api route not working over https

I have a node server running on port 3000 to serve api request.
this works fine...
http://www.skoolaide.com:3000/api/accounts
this does not.
https://www.skoolaide.com:3000/api/accounts
Can someone tell me why? If you go to https://www.skoolaide.com, the ssl cert is configured correctly, but do I need to do something on the node side?
this is my server js file. Please note: is only runs the middleware(api) on port 3000. For some reason I cannot access the middleware via https...
'use strict';
var express = require('express');
var router = express.Router();
var app = express();
var http = require('http');
var open = require('open');
var cors = require('cors');
var path = require('path');
var morgan = require('morgan');
var errorhandler = require('errorhandler');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var config = require('./config/config');
var jwt = require('jsonwebtoken');
var compression = require('compression');
var runMiddleware = require('run-middleware')(app);
var fs = require('fs');
var readline = require('readline');
var google = require('googleapis');
var googleAuth = require('google-auth-library');
var multer = require('multer');
var node_xj = require("xls-to-json");
var moment = require('moment');
var async = require('async');
var btoa = require('btoa');
var sharp = require('sharp');
var students = require("./middleware/students.api");
var accounts = require("./middleware/accounts.api");
var messages = require("./middleware/messages.api");
var advocates = require("./middleware/advocates.api");
var authenticate = require("./middleware/authenticate.api");
var email = require("./middleware/email.api");
var text = require("./middleware/text.api");
var colleges = require("./middleware/colleges.api");
var amazon = require("./middleware/amazon.api");
var rewards = require("./middleware/rewards.api");
var files = require("./middleware/files.api");
var validations = require("./middleware/validations.api");
var points = require("./middleware/points.api");
var notifications = require("./middleware/notifications.api");
var notificationsMap = require("./middleware/notificationsMap.api");
var trivia = require("./middleware/trivia.api");
var tasks = require("./middleware/rewardgoals.api");
var classes = require("./middleware/classes.api");
var connections = require("./middleware/connections.api");
var badges = require("./middleware/badges.api");
var fixpasswords = require("./middleware/fixpasswords.api");
var Files = require('./models/files');
mongoose.connect(config.database);
process.on('SIGINT', function() {
mongoose.connection.close(function () {
console.log('Mongoose disconnected on app termination');
process.exit(0);
});
});
// use body parser so we can get info from POST and/or URL parameters
app.use(bodyParser.json({
limit: '50mb'
}));
app.use(bodyParser.urlencoded({
limit: '50mb',
extended: true
}));
app.set('uploads', path.join(__dirname, 'uploads'));
app.get("/uploads/*", function (req, res, next) {
res.sendFile(__dirname + req.url);
});
var whitelist = ['https://www.skoolaide.com', 'https://skoolaide.com'];
var corsOptionsDelegate = function (req, callback) {
var corsOptions;
if (whitelist.indexOf(req.headers['origin']) !== -1) {
corsOptions = { origin: true, credentials: true } // reflect (enable) the requested origin in the CORS response
} else {
corsOptions = { origin: false, credentials: false } // disable CORS for this request
}
callback(null, corsOptions) // callback expects two parameters: error and options
}
app.use(cors(corsOptionsDelegate));
//this is used because the body parser removes undefined values from the database.
app.set('json replacer', function (key, value) {
// undefined values are set to `null`
if (typeof value === "undefined") {
return null;
}
return value;
});
app.use(multer({
dest: './uploads/',
rename: function (fieldname, filename) {
return filename.replace(/\W+/g, '-').toLowerCase() + Date.now()
},
onFileUploadStart: function (file) {
//console.log(file.fieldname + ' is starting ...')
},
onFileUploadData: function (file, data) {
//console.log(data.length + ' of ' + file.fieldname + ' arrived')
},
onFileUploadComplete: function (file) {
//console.log(file.fieldname + ' uploaded to ' + file.path)
}
}).any());
// "files" should be the same name as what's coming from the field name on the client side.
app.post("/api/upload", function (req, res) {
//console.log(req.files[0].mimetype)
fs.readFile(req.files[0].path, function (err, data) {
var newPath = __dirname + "/uploads/" + req.headers["account_id"] + '_' + moment().format('MM_DD_YYYY_HH-mm-ss') + '_' + req.files[0].originalname.replace(/[^a-zA-Z0-9.]/g, '_');
var readPath = "/uploads/" + req.headers["account_id"] + '_' + moment().format('MM_DD_YYYY_HH-mm-ss') + '_' + req.files[0].originalname.replace(/[^a-zA-Z0-9.]/g, '_');
if(req.files[0].mimetype.indexOf('image') > -1){
sharp(data)
.rotate()
.resize(800, 800)
.max()
.toFile(newPath, function(err, info){
var file = new Files();
file.owner = req.headers.account_id || null;
file.classId = req.body.classId || null;
file.rewardGoalId = req.body.rewardGoalId || null;
file.avatarId = req.body.avatarId || null;
file.mimeType = req.files[0].mimetype || null;
file.originalName = req.files[0].originalname || null;
file.path = readPath;
file.save(function (err, newFile) {
if (err) {
res.send(err);
} else {
res.send({ success: true, data: newFile }).end();
}
});
});
} else{
//not an image file...
var newPath = __dirname + "/uploads/" + req.headers["account_id"] + '_' + moment().format('MM_DD_YYYY_HH-mm-ss') + '_' + req.files[0].originalname.replace(/[^a-zA-Z0-9.]/g, '_');
//console.log('Writing file: ', newPath);
fs.writeFile(newPath, data, function (err) {
var readPath = "/uploads/" + req.headers["account_id"] + '_' + moment().format('MM_DD_YYYY_HH-mm-ss') + '_' + req.files[0].originalname.replace(/[^a-zA-Z0-9.]/g, '_');
//console.log(readPath)
var file = new Files();
file.owner = req.headers.account_id || null;
file.classId = req.body.classId || null;
file.rewardGoalId = req.body.rewardGoalId || null;
file.profileId = req.body.profileId || null;
file.mimeType = req.files[0].mimetype || null;
file.originalName = req.files[0].originalname || null;
file.path = readPath;
file.save(function (err, newFile) {
if (err) {
res.send(err);
} else {
res.send({ success: true, data: newFile }).end();
}
});
});
}
});
});
app.use(express.static('www'))
var a = ['/'];
//all get requests resolve to index.
app.get(a, function (req, res) {
console.log('This is a test', req.originalUrl, req.url);
res.setHeader('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.setHeader('Expires', '-1');
res.setHeader('Pragma', 'no-cache');
res.setHeader('X-UA-Compatible', 'IE=Edge,chrome=1');
res.setHeader('Judson', 'Rocks!');
if (req.originalUrl == '/') {
res.sendFile('www/index.html', {root: __dirname});
}
});
app.set("superSecret", config.secret)
//var upload = require("./middleware/upload.api");
app.use('/api', router);
app.use('/api/students', students);
app.use('/api/accounts', accounts);
app.use('/api/messages', messages);
app.use('/api/advocates', advocates);
app.use('/api/authenticate', authenticate);
app.use('/api/email', email);
app.use('/api/text', text);
app.use('/api/colleges', colleges);
app.use('/api/amazon', amazon);
app.use('/api/rewards', rewards);
app.use('/api/files', files);
app.use('/api/validate', validations);
app.use('/api/points', points);
app.use('/api/notifications', notifications);
app.use('/api/notificationsMap', notificationsMap);
app.use('/api/trivia', trivia);
app.use('/api/rewardgoals', tasks);
app.use('/api/classes', classes);
app.use('/api/badges', badges);
app.use('/api/connections', connections);
app.use('/api/fixpasswords', fixpasswords);
/**SERVER*************************************/
// all environments
app.set('port', process.env.PORT || 3000);
app.engine('html', require('ejs').renderFile);
// express/connect middleware
app.use(morgan('dev'));
app.use(compression()); //use compression
// development only
if ('development' === app.get('env')) {
app.use(errorhandler());
}
/**END SERVER*************************************/
var cluster = require('cluster');
if (cluster.isMaster) {
var numWorkers = require('os').cpus().length;
console.log('Master cluster setting up ' + numWorkers + ' workers...');
for (var i = 0; i < numWorkers; i++) {
cluster.fork();
}
cluster.on('online', function (worker) {
console.log('Worker ' + worker.process.pid + ' is online');
});
cluster.on('exit', function (worker, code, signal) {
console.log('Worker ' + worker.process.pid + ' died with code: ' + code + ', and signal: ' + signal);
console.log('Starting a new worker');
cluster.fork();
});
} else {
app.listen(app.get('port'),
function () {
console.log('myApp server listening on port ' + app.get('port'));
});
}
Your code only starts an HTTP server. This line:
app.listen(app.get('port'))
starts only an http server. If you want support for HTTPS, you have to also start an HTTPS server and you have to start it on a different port. The express doc for app.listen() shows you how to do that and it's basicaly like this:
var express = require('express');
var https = require('https');
var http = require('http');
var app = express();
http.createServer(app).listen(80);
https.createServer(options, app).listen(443);
where options contains your SSL certificates and where you fill in the desired port numbers for your http server and your https server.
If you were operating under the illusion that you don't need a port number in the browser in order to access both an http and https server, that's because the browser fills in a default port number for you. If you just type an http URL, it uses port 80. If you type an https URL with no port, it uses port 443. If you're not on those two specific ports, you have to specify the desired port in the browser URL. In either case, you need a separate server on separate ports for both http and https.
Use var https = require("https"); not
Var http = require ("http");

Nodejs - Disable or destroy vhost subdomains/proxies

I have the follow code which dynamically proxies a website to a subdomain.
var app = require('express')();
var proxy = require('express-http-proxy');
var vhost = require('vhost');
app.get('/make', function (req, res) {
app.use(vhost('sub1.mysite.com', proxy("www.example.com");
res.send("Proxy Created");
});
app.get('/destroy', function (req, res) {
// Disable or Destroy the subdomain/proxy created above
// ???
});
app.listen(8080);
How would I disable or destroy this newly created proxy/subdomain from the /destroy route?
app.js
var Ex = require('express');
var app = Ex();
app.listen(8080);
var DomainProxy = require('./DomainProxy').DomainProxy;
// Pass express instance 'app' into dp,
// dp will create a vhost router internally
var dp = new DomainProxy(app);
var proxies = [
{ domain: '1.local', site: 'http://www.bing.com' },
{ domain: '2.local', site: 'http://samanthagooden.com' },
{ domain: '3.local', site: 'http://www.courtleigh.com' },
{ domain: '4.local', site: 'http://example.com' }
];
dp.create(proxies);
app.get('/make', function (req, res) {
var subDomain = 'sub1.mysite.com';
var site = 'http://www.example.com'; // site need to contain 'http://', else dp link rewrite will not work correctly
dp.create([{domain:subDomain,site:site}]);
res.send("Proxy Created");
});
app.get('/destroy', function (req, res) {
var subDomain = 'sub1.mysite.com';
dp.destroy(subDomain);
res.send("Proxy Destroyed");
});
DomainProxy.js
I also incorporated my answer from Nodejs - BrowserSync running on express server
var proxy = require('express-http-proxy');
var url = require('url');
var vhost = require('vhost');
var Ex = require('express');
var DomainProxy = (function () {
function DomainProxy(app) { // DomainProxy
this.list = []; // vhost list array
this.router = Ex.Router();
app.use(this.router);
}
DomainProxy.prototype.create = function (proxies) {
proxies.forEach(p => {
// Destroy old proxy first
this.destroy(p.domain);
this.list.push(p.domain); // Add domain into list
this.router.use(vhost(p.domain, proxy(p.site, {
forwarDomainProxyath: (req, res) => url.parse(req.url).path,
intercept: (rsp, data, req, res, callback) => {
if (res._headers['content-type']) {
var contentType = res._headers['content-type'];
// console.log(contentType);
if (
contentType.indexOf('text') !== -1 ||
contentType.indexOf('javascript') !== -1
) {
// Rewrite links
var reg = new RegExp(p.site, 'g');
res.send(data.toString().replace(reg, ''));
} else {
res.send(data);
}
} else {
res.send(data);
}
}
})));
})
};
DomainProxy.prototype.destroy = function (domain) {
var i = this.list.indexOf(domain);
if (i !== -1) {
this.list.splice(i, 1);
this.router.stack.splice(i, 1);
}
};
return DomainProxy;
} ());
exports.DomainProxy = DomainProxy;

Resources