Something is calling POST /inform in nodejs express project - node.js

I have a pretty simple nodejs project that uses express. When I start this project locally I have noticed that something is calling a POST to /inform about every 30 seconds. I'd like to know what's calling inform and what the purpose is.
I'm new to node. Is this normal? I haven't implemented a route for this call so it causes a 404.
Here's my main app:
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const fileUpload = require('express-fileupload');
const app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(fileUpload());
// routes
const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use((req, res, next) => {
console.log(req)
next(createError(404));
});
// error handler
app.use((err, req, res) => {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
// Start the server
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
console.log('Press Ctrl+C to quit.');
});
In my console, I see this about every 30 seconds:
POST /inform 404 14.002 ms - 2158
POST /inform 404 13.910 ms - 2158
POST /inform 404 31.536 ms - 2158
EDIT:
Thank you for the comments. I changed my express port to 8000 and it no longer happens. So something on my local machine is looping and posting to localhost:8080/inform. I'll have to trace this down.

I have a Ubiquity Unify network stack at home. After running (and stopping) the Unifi Controller on my laptop, all my Unify devices continue to send a POST <laptop IP>:8080/inform.
My own application was getting its logged filled up with the same unknown route: "/inform" error.
Solutions:
Choose a different port
Bind your application to 'localhost' instead of '0.0.0.0'
Get a dedicated controller device, like a Raspberry Pi or Unifi Cloud Key

This could be pretty much any application on your PC or even other systems in your local network, if you are listening on public addresses.
You could implement the route to see if the request header/body give any hint as to where this is coming from.
It may also be possible for external software such as Wireshark to monitor network calls to localhost, including their source.
Otherwise use a different port where no one is sending periodic pings to.

Related

how to redirect large file requests through node.js to a different server using request

I have a node.js server that is forwarding API requests through to another server on a different port (so that auth cookies and the like make it across), and this has all worked great until the client has needed to upload a large file (> 100mb).
When I try to do that, if the file is over ~30mb the request never even reaches the far server (which will happily accept large files when connected to directly), so I'm pretty sure it's dying in node ... somewhere.
"use strict";
const http = require("http");
const port = process.env.FRONTEND_PORT || 13370;
const path = require("path");
const request = require("request");
const express = require("express");
const app = express();
const staticPath = path.join(__dirname, "/dist/");
app.set("port", port);
// Attempt to set some ridiculously high limits on things
app.use(express.urlencoded({parameterLimit: 1000000, limit: '10gb', extended: true}));
app.use(express.json({limit: '10gb'}));
// Serve files from this location
app.use(express.static(staticPath));
app.use(
'/api',
(req, res) =>
{
var url = "https://localhost:12121/api" + req.url;
req.pipe(request(url)).pipe(res);
console.log(res.statusCode);
console.log(res.statusMessage);
});
// If we hit any path that doesn't exist, instead serve up index.html and let react router handle it
app.get("*", (req, res) => { res.sendFile(path.join(__dirname, "/dist/index.html")); });
app.listen(app.get("port"), () => { console.log("listening"); });
As you can see, I've tried bumping up the limits (based on other SO answers I've seen) using:
app.use(express.urlencoded({parameterLimit: 1000000, limit: '10gb', extended: true}));
app.use(express.json({limit: '10gb'}));
...but this doesn't seem to be helping me in this case. I'm sure I'm missing something obvious, but I am extremely new to node.js (and, honestly, web development in general).
(It's also worth noting, the file is being sent through a basic XMLHttpRequest as POST, sending a File object through - nothing fancy going on there)

Using Twig render engine with Express

I've decided to make the jump from building my websites with slim, php and twig to using node.js and express.
To limit the shock of changing my whole process I want to keep using slim as the template engine instead of the default jade.
I have tried to change the code to change the render engine over to twig but now I am getting the following error message. Does anyone know what I am doing wrong? I am at a loss as to why it is not working. Any help would be very helpful.
Error message:
Error: Failed to lookup view "error" in views directory "/var/www/html/SocialTrackers/app/views"
at Function.render (/var/www/html/SocialTrackers/app/node_modules/express/lib/application.js:580:17)
at ServerResponse.render (/var/www/html/SocialTrackers/app/node_modules/express/lib/response.js:1008:7)
at /var/www/html/SocialTrackers/app/app.js:38:7
at Layer.handle_error (/var/www/html/SocialTrackers/app/node_modules/express/lib/router/layer.js:71:5)
at trim_prefix (/var/www/html/SocialTrackers/app/node_modules/express/lib/router/index.js:315:13)
at /var/www/html/SocialTrackers/app/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/var/www/html/SocialTrackers/app/node_modules/express/lib/router/index.js:335:12)
at next (/var/www/html/SocialTrackers/app/node_modules/express/lib/router/index.js:275:10)
at Layer.handle_error (/var/www/html/SocialTrackers/app/node_modules/express/lib/router/layer.js:67:12)
at trim_prefix (/var/www/html/SocialTrackers/app/node_modules/express/lib/router/index.js:315:13)
This is the code I am currently running. Other than changes to shift over the using twig as the render engine, I have made no other changes to the initial express install.
app.js
var createError = require('http-errors');
var twig = require("twig");
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, './views'));
app.set('view engine', 'twig');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/bootstrap', express.static(__dirname + '/node_modules/bootstrap/dist/'))
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
Additionally, I am the following file structure (default express)
app
--bin
--node_modules
--public
--routes
----index.js
----users.js
--views
----error.twig
----index.twig
----landingpage.twig
--app.js
Took me a few days to figure this one out.
As I said I'm new to Nodejs and express so I believe I made a very rookie mistake. I thought I would post the solution incase anyone else has this issue.
I was running the node server on a ec2 instance. While I was updating the code it turns out I had to restart the node server as well.
I'm using pm2 to keep the server running all the time so for me the solution was to run the following command.
pm2 restart [namespace of server]
Note: for me, I never gave the server a namespace so it was simply 'default'. If you don't know the namespace you can find it by running 'pm2 list'
pm2 restart default

Cannot remote connect to gremlin server from express (Node.js)

I have set up a Janusgraph Docker instance (janusgraph/janusgraph:latest - Lucene/BerkeleyDB/Tinkerpop/Gremlin Server) in a Linux VM in my datacentre. On the same VM and on my laptop, I've run the same JanusGraph Docker image but run the gremlin console (adjusting conf/remote.yaml to point to the gremlin server) and successfully imported air-routes.graphml into the server instance. I can also successfully query the air-routes data with some simple traversals.
In summary: I'm confident that the server is running, has data, can be remotely connected and respond to traversals.
I have been stuck on the next step for a long, long time:
I have created a basic Node express stub:
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var gremlin = require('gremlin');
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const traversal = gremlin.process.AnonymousTraversalSource.traversal;
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
const g = traversal().withRemote(
new DriverRemoteConnection('ws://mygremlinserverhost:8182/gremlin'));
This fails with the following:
npm run start
greg:air-routes/ $ npm run start [21:07:39]
Debugger attached.
> air-routes#0.0.0 start
> node ./bin/www
Debugger attached.
Waiting for the debugger to disconnect...
node:internal/process/promises:227
triggerUncaughtException(err, true /* fromPromise */);
^
Error: connect ECONNREFUSED <The IP of the VM>:8182
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1138:16) {
errno: -61,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '<The IP of the VM>',
port: 8182
}
Waiting for the debugger to disconnect...
greg:air-routes/ $
(Also, I don't understand this notification, or whether it's the cause (or effect) of the inability to connect - What am I doing wrong or not doing. Please assume an idiot novice who needs the basics explaining. Thanks, I think? ;o) )
node:internal/process/promises:227
triggerUncaughtException(err, true /* fromPromise */);
^
I believe the inability to connect was because I had not specified a docker port mapping to the OS port on the server's docker run command.
Should have been (with the port mapping switch -p)
docker run --name janusgraph-default -p 8182:8182 --volume /myLocalDir/gremlin:/dataImports janusgraph/janusgraph:latest

How to fix recent node.js errors from my apps?

const express = require("express");
const path = require("path");
var hbs = require("express-handlebars");
const morgan = require("morgan");
const middlewares = require("./middlewares/middlewares");
const PORT = process.env.PORT || 3002;
var app = express();
// setup static file service
app.use(express.static(path.join(__dirname, "static")));
//Setup app port
app.set("port", process.env.PORT || PORT);
// setup handlebars and the view engine for res.render calls
app.set("view engine", "html");
app.engine(
"html",
hbs({
extname: "html",
defaultView: "default",
layoutsDir: __dirname + "/views/layouts/",
partialsDir: __dirname + "/views/partials/"
})
);
app.get("/", (req, res) => {
res.send("Hello world");
});
app.use(morgan("common"));
app.use(middlewares.errorHandler);
app.use(middlewares.ignoreFavicon);
app.use(middlewares.notFound);
var server = app.listen(app.get("port"), () =>
console.log(`Server started...Listening on port: ${PORT}`)
);
This is my basic server setup. Whenever I request a route I get the following errors in my console. I had to add middleware ignoring this route as well. "GET /favicon.ico HTTP/1.1"
Uncaught TypeError: Cannot convert undefined or null to object
at Function.keys ()
at contentscript.bundle.js:72
at S.Object.isExtensible.Object.isExtensible.e.___hb.e.___hb (contentscript.bundle.js:29)
[Honeybadger] Unable to send error report: no API key has been configured.
is there more of a stack trace? Also, when you say "console", do you mean in the Node console, or in the browser console? It looks like the log message is from our client-side JavaScript package, which I would expect in the browser. If you're catching errors in your Node application, you should use our Node.js package.
In honeybadger.js, ___hb can sometimes appear in stack traces because the function was wrapped by our library, but the error is often still in your project code. I would suggest configuring Honeybadger with an API key if you're trying to diagnose errors in production.
Related docs:
https://docs.honeybadger.io/lib/javascript/index.html
https://docs.honeybadger.io/lib/node.html

Express + React + Heroku can't find a file

i want to get my app working on Heroku, But there is on problem. Heroku server can't find the images. On developmnt everything is working correctly. Images are loaded propertly. On heroku everything works correctly. Asking for json /api/articles gives in result json. Router also working. But images gives not found. In tag and fetch() .
code => https://github.com/bartek-fecko/cinema-app
The image file exists on heroku(i looked to the bash on heroku.com)
var express = require('express');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, '../client/build')));
app.use('/api', indexRouter);
app.use('/api/users', usersRouter);
app.use(express.static(path.join(__dirname, '/assets/images')));//i'm sending a file
app.get('/images/:fileName', (req, res) => {
res.sendFile(path.join(__dirname, `/assets/images/${req.params.fileName}`)); //here also i'm sending a file
});
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, '../client/build/index.html'));
});
module.exports = app;
<P.Wrapper>
<img src="spider-man-far-from-home.jpg" alt="no img"/>
<img src="/images/spider-man-far-from-home.jpg" alt="no img"/>
</P.Wrapper>
I'm getting a 404 error. And I tried everything and it doesn't work. The same for fetching fetch('/images/ ...') and so on.
From your code, you are having 2 servers:
Backend server (inside src)
Frontend server (inside client)
Since when you are starting them, they are of different servers, meaning that they either would be running on different IPs or the same IP but different Ports.
This means that your fetch of the image in the client's App.tsx would most likely be referencing the wrong host.
fetch('/images/spider-man-far-from-home.jpg')
Try using a full URL with both Host and Port in the fetch method and see if it works for you.
Yeah. woow. It's working now. The problem was exactly like Yee Hui Poh said. So in production i have to fetch to locallhost:8080/images/spider-man-far-from-home.jpg
where 8080 is backend server.

Resources