I'm trying to use kue for scheduled jobs on my Parse Server (hosted on heroku). For now I've modified my index.js file like so as stated in the several tutorials I found about Kue :
var express = require('express')
, kue = require('due')
, redis = require('redis');
var ParseServer = require('parse-server').ParseServer;
var databaseUri = process.env.DATABASE_URI || process.env.MONGOLAB_URI;
if (!databaseUri) {
console.log('DATABASE_URI not specified, falling back to localhost.');
}
var api = new ParseServer({
databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
appId: process.env.APP_ID || 'myAppId',
masterKey: process.env.MASTER_KEY || '',
serverURL: process.env.SERVER_URL
});
// Client-keys like the javascript key or the .NET key are not necessary with parse-server
// If you wish you require them, you can set them as options in the initialization above:
// javascriptKey, restAPIKey, dotNetKey, clientKey
// connect to REDIS
var client = redis.createClient(process.env.REDIS_URL);
var app = express();
// Serve the Parse API on the /parse URL prefix
var mountPath = process.env.PARSE_MOUNT || '/parse';
app.use(mountPath, api)
.use(kue.app); // wire up Kue (see /active for queue interface)
// Parse Server plays nicely with the rest of your web routes
app.get('/', function(req, res) {
res.status(200).send('I dream of being a web site.');
});
var port = process.env.PORT || 1337;
app.listen(port, function() {
console.log('parse-server-example running on port ' + port + '.');
});
I've found out that the app crashes at the line : .use(kue.app). Here is the error I get :
Starting process with command `node index.js`
parse-server-example running on port 22995.
/app/node_modules/parse-server/lib/index.js:298
throw err;
^
Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
at Object.exports._errnoException (util.js:890:11)
at exports._exceptionWithHostPort (util.js:913:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1057:14)
Process exited with status 7
State changed from starting to crashed
I don't know why this is happening.
The line : .use(ku.app) can be removed. And all that is needed is to add :
var jobs = kue.createQueue({ redis: process.env.REDIS_URL })
to access the current queue.
Hope it'll helps somebody.
Related
So I am fairly new to the backend. Anyway, I want to create an API that I can use in the front-end, the error I am facing when I try to send a request to the localhost:5000/elements Postman is giving me Error: connect ECONNREFUSED 127.0.0.1:5000 if someone could help me it would be awesome. Thanks
var fs=require('fs');
var data=fs.readFileSync('books.json');
var elements=JSON.parse(data);
const express = require("express");
const app = express();
const cors=require('cors');
const Port = 5000
app.listen(process.env.Port, () => console.log("Server Start at " + Port));
app.use(express.static('public'));
app.use(cors());
app.get('/elements',alldata);
function alldata(request,response)
{
response.send(elements);
}
app.get('/elements/:element/',searchElement);
function searchElement(request,response)
{
var word=request.params.element;
word=word.charAt(0).toUpperCase()+word.slice(1).toLowerCase();
console.log(word);
console.log(elements[word]);
if(elements[word])
{
var reply=elements[word];
}
else
{
var reply={
status:"Not Found"
}
}
console.log(reply.boil);
response.send(reply);
}
This problem usually happens if you forget to run npm start.
Either way, I recommend moving the app.listen to the bottom of the code. It helps with readability, and it will mount all of code before running the Express server.
Your process.env.Port is also undefined. Change it to const port = process.env.Port || 5000 so you can get a fallback value. Change it also in the app.listen.
Then, define allData and searchElement so they are located before the app.get('/elements'). Finally, after you have done all of this, make sure that the request type in Postman is GET.
process.env.Port is unrelated to Port.
It's
app.listen(Port, () => console.log("Server Start at " + Port));
I have a 4 core CPU with 8 logical processors, which in this code creates 8 workers and 1 master process. When a socket connection is formed, it tends to connect to the last worker, CPU 8. Does using this method automatically add a Load Balancer, or would I need to add it in? Is there a way to test if the Load Balancer is working? I've tried to add 100s of clients, but they all connect to CPU 8 - not sure if it could be because there is barely any process handling in this instance
Simple Node.js Clustering
const os = require('os'),
cluster = require('cluster'),
cores = os.cpus();
var clusterCount = 0;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers
for (let i = 0; i < cores.length; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
}
else {
const http = require('http'),
express = require('express'),
socketio = require('socket.io'),
process = require('process');;
var cpu = cores[clusterCount];
var app = express();
var port = process.env.PORT || process.argv[2] || 8080;
var server = app.listen(port);
var io = socketio(server);
io.adapter(socketioRedis({ host: config.redis_host, port: config.redis_port }));
io.on('connection', (socket) => {
console.log(`User ${socket.id} connected to worker ${process.pid}`);
});
console.log(`Worker ${process.pid} started on port: ${port} | ${cpu.model}`);
clusterCount++;
}
It depends on a couple of points. https://nodejs.org/api/cluster.html#cluster_how_it_works
Does using this method automatically add a Load Balancer?
The master will handle the load balancing.
sure if it could be because there is barely any process handling in this instance
It might be that CPU 8 is not that busy and can still handle the load. There are two strategies which also depend on the OS you are using.
I am trying to capture client ipaddress for traffic visiting my localhost:8080. I am using the following modules and the node.js application looks like this
var connect = require('connect');
var http = require('http');
var net = require('net');
var express = require('express');
var app = express();
var app = connect();
// require request-ip and register it as middleware
var requestIp = require('request-ip');
// you can override which attirbute the ip will be set on by
// passing in an options object with an attributeName
app.use(requestIp.mw({ attributeName : 'myCustomAttributeName' }))
// respond to all requests
app.use(function(req, res) {
// use our custom attributeName that we registered in the middleware
var ip = req.myCustomAttributeName;
console.log(ip);
fs.appendFile('iplist.csv', ip, 'utf8', function (err) {
if (err) {
console.log('Some error occured - file either not saved or corrupted file saved');
} else{
console.log('It\'s saved!');
}
});
// https://nodejs.org/api/net.html#net_net_isip_input
// var ipType = net.isIP(ip); // returns 0 for invalid, 4 for IPv4, and 6 for IPv6
// res.end('IP address is ' + ip + ' and is of type IPv' + ipType + '\n');
});
//create node.js http server and listen on port
app.listen(8080);
Is there any way I can listen to the already existing server without creating my own, hence avoiding the conflict of two servers fighting for the same port. I am new to node.js. Any help will be great. Thank you!
I am currently using crypto.js module to hash things. It was working for a while then I started getting this error:
Here is the foundation of my server:
process.stdout.write('\033c'); // Clear the console on startup
var
express = require("express"),
app = express(),
http = require("http").Server(app),
io = require("socket.io")(http),
path = require("path"),
colorworks = require("colorworks").create(),
fs = require("fs"),
crypto = require("crypto");
function md5(msg){
return crypto.createHash("md5").update(msg).digest("base64");
}
function sha256(msg) {
return crypto.createHash("sha256").update(msg).digest("base64");
}
http.listen(443, function(){
// Create the http server so it can be accessed via 127.0.0.1:443 in a web browser.
console.log("NJ project webserver is running on port 443.");
// Notify the console that the server is up and running
});
app.use(express.static(__dirname + "/public"));
app.get("/", function(request, response){
response.sendFile(__dirname + "/public/index.html");
});
I am aware that these functions are creating the problem:
function md5(msg){
return crypto.createHash("md5").update(msg).digest("base64");
}
function sha256(msg) {
return crypto.createHash("sha256").update(msg).digest("base64");
}
The problem being, if these functions don't work (which they don't anymore), roughly 200 lines of code will go to waste.
This error is triggered by attempting to hash a variable that does not exist:
function md5(msg){
return crypto.createHash("md5").update(msg).digest("base64");
}
function sha256(msg) {
return crypto.createHash("sha256").update(msg).digest("base64");
}
md5(non_existent); // This variable does not exist.
What kind of data are you trying to hash ? Where does it come from ?
I would check the value of msg first then I would try :
crypto.createHash('md5').update(msg.toString()).digest('hex');
You could also use these packages instead:
https://www.npmjs.com/package/md5
https://www.npmjs.com/package/js-sha256
I am trying to have my nodejs server run on start up of my Windows Forms application using Edgejs. I am encountering a hiccup when referencing my api module. Is this an error within EdgeJS that I cannot control and need to find a way around? What if I made an API controller instead? NOTE: When using this same exact code in my web application it functions as expected.
c# start up file
public static async void Start()
{
var createHttpServer = Edge.Func(File.ReadAllText("../../../../server/server.js"));
await createHttpServer(new
{
port = 3333,
});
}
static void Main(string[] args)
{
Task.Run((Action)Start).Wait();
Application.Run(new Form1());
}
server.js
"use strict";
var http = require('http');
var express = require('express'); // web framework
var cors = require('cors'); // middleware for express
var bodyParser = require('body-parser'); // parsing module
var path = require('path');
var edge = require('edge');
var api = require('./api.js');
return function (options, cb) {
var app = express();
app.use(bodyParser.urlencoded({'extended': 'true'})); // parse application/x-www-form-urlencoded
app.use(bodyParser.json());
app.get('/api/getFullCatalog', api.getFullCatalog());
// IMPORTANT!: This rule has to be last rule for routing as it has very common definition
app.all('/*', function (request, response) {
var fileName, serverPath;
console.log('Send: ' + __dirname + request.url);
serverPath = path.resolve(__dirname + '../../../../../../server/');
fileName = serverPath + request.url;
response.sendFile(fileName);
});
// start server listening on defined port
app.listen(options.port);
console.log('The NodeJS server is ready.');
};
I can start the server and run when I remove said module
var api = require('./api');
Here is the error
{"Error: Cannot find module './api.js'\n
at Function.Module._resolveFilename (module.js:336:15)\n
at Function.Module._load (module.js:286:25)\n
at Module.require (module.js:365:17)\n
at require (module.js:384:17)\n
at eval (eval at <anonymous> (C:\\TechSpikeUDC\\TechSpikeUDC\\WpfApplication1\\bin\\x86\\Debug\\edge\\double_edge.js:34:28), <anonymous>:9:11)\n
at compileFunc (C:\\TechSpikeUDC\\TechSpikeUDC\\WpfApplication1\\bin\\x86\\Debug\\edge\\double_edge.js:35:16)"}
(C:\\TechSpikeUDC\\TechSpikeUDC\\TechSpikeUDC\\bin\\x86\\Debug\\edge\\double_edge.js:34:28), <anonymous>:37:15)"} System.Exception
Again this only happens when used in the thick client c# application.
Any help is appreciated... Thank you.