Log4js - Disable logs for http calls - node.js

I'm using log4js to log messages. For some reason, the logger automatically logs the http calls, and I can't find a way to disable it. Here is my code:
// Initialize the log4js logger by configurations parameters and appenders.
log4js.configure({
appenders: {
app: {
type: 'file',
filename: 'logs/app.log',
pattern: '-yyyy-MM-dd',
maxLogSize: LOGS_FILES_MAXIMUM_SIZE
},
mq: {
type: '#log4js-node/rabbitmq',
host: LOG_SERVER_HOST_ADDRESS,
port: LOG_SERVER_HOST_PORT,
username: LOG_SERVER_USERNAME,
password: LOG_SERVER_PASSWORD,
routing_key: LOG_SERVER_ROUTING_KEY,
exchange: LOG_SERVER_EXCHANGE_NAME,
mq_type: 'direct',
durable: true
}
},
categories: {
default: { appenders: ['app'], level: LogLevel.ALL },
development: { appenders: ['app'], level: LogLevel.ALL },
production: { appenders: ['app', 'mq'], level: LogLevel.ALL }
}
});
this.log = log4js.getLogger('production');
and in app.js
this.app.use(log4js.connectLogger(log4js.getLogger('production'), { level: 'off' }));
For some reason the logger automatically log messages like this:
[2019-06-19T16:02:16.182] [OFF] development - ::1 - - "POST
/api/log/logMessage HTTP/1.1" 200 2
"http://localhost:3000/?recording-session-id=b983cb82-4812-4c43-87b3-a17e7ed1f6b4&environment=data-center"
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,
like Gecko) Chrome/75.0.3770.90 Safari/537.36"
[2019-06-19T16:02:16.182] [OFF] development - ::1 - - "OPTIONS
/api/stream/getStreamData HTTP/1.1" 204 0
"http://localhost:3000/?recording-session-id=b983cb82-4812-4c43-87b3-a17e7ed1f6b4&environment=data-center"
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,
like Gecko) Chrome/75.0.3770.90 Safari/537.36"
[2019-06-19T16:02:16.189] [OFF] development - ::1 - - "POST
/api/stream/getStreamData HTTP/1.1" 200 999
"http://localhost:3000/?recording-session-id=b983cb82-4812-4c43-87b3-a17e7ed1f6b4&environment=data-center"
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,
like Gecko) Chrome/75.0.3770.90 Safari/537.36"

In my code it had to do with the following lines in app.js:
const logger = require('morgan');
app.use(logger('dev'));
I deleted these lines so as to only use log4js, an not Morgan.

Related

Trying to get Twitch user name & check if its valid keeping running into an error Parse the body

I'm trying to parse so I can check if the username is valid or not. Though I have little to no experience working with JSON parasing in NodeJS. I'd appreciate some help on this issue. This has been a struggle moving over to NodeJS and trying to work with APIs and parasing them.
Here's the code and here is the error
userid = body[0]['data']['user']['id'];
^
TypeError: Cannot read properties of undefined (reading 'data')
at Request._callback (C:\Users\Tommy\Desktop\Misc\Node Projects\Discord Twitch Username Check\index.js:141:25)
at Request.self.callback (C:\Users\Tommy\Desktop\Misc\Node Projects\Discord Twitch Username Check\node_modules\request\request.js:185:22)
at Request.emit (node:events:390:28)
at Request.<anonymous> (C:\Users\Tommy\Desktop\Misc\Node Projects\Discord Twitch Username Check\node_modules\request\request.js:1154:10)
at Request.emit (node:events:390:28)
at IncomingMessage.<anonymous> (C:\Users\Tommy\Desktop\Misc\Node Projects\Discord Twitch Username Check\node_modules\request\request.js:1076:12)
at Object.onceWrapper (node:events:509:28)
at IncomingMessage.emit (node:events:402:35)
at endReadableNT (node:internal/streams/readable:1343:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
function getUser(username) {
const opts = {
"url": `https://gql.twitch.tv/gql`,
headers: {
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="98", "Google Chrome";v="98"',
'Accept-Language': 'en-US',
'sec-ch-ua-mobile': '?0',
'Client-Version': '7b9843d8-1916-4c86-aeb3-7850e2896464',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36',
'Content-Type': 'text/plain;charset=UTF-8',
'Client-Session-Id': '51789c1a5bf92c65',
'Client-Id': 'kimne78kx3ncx6brgo4mv6wki5h1ko',
'X-Device-Id': 'xH9DusxeZ5JEV7wvmL8ODHLkDcg08Hgr',
'sec-ch-ua-platform': '"Windows"',
'Accept': '*/*',
'Origin': 'https://www.twitch.tv',
'Sec-Fetch-Site': 'same-site',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Dest': 'empty',
'Referer': 'https://www.twitch.tv/',
},
body: '[{"operationName": "WatchTrackQuery","variables": {"channelLogin": "'+username+'","videoID": null,"hasVideoID": false},"extensions": {"persistedQuery": {"version": 1,"sha256Hash": "38bbbbd9ae2e0150f335e208b05cf09978e542b464a78c2d4952673cd02ea42b"}}}]'
}
request(opts, (err, res, body) => {
body = JSON.parse(body);
userid = body[0]['data']['user']['id'];
if(userid === 0){
return "Invalid Username"
}else{
return userid;
}
});
};

How can I detect browser details in NestJs/NodeJs

I am trying to integrate device-detector npm module in my application in order to detect the browser details. For that I am using this module npm i device-detector-js
I have integrated as it is code snippet in my code.
Below is my code:
app.controller.ts
import { Controller, Get, Req } from '#nestjs/common';
import { AppService } from './app.service';
#Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
#Get()
getHello(#Req() req): string {
console.log(req.headers);
return this.appService.getHello();
}
}
app.service.ts
import { Inject, Injectable } from '#nestjs/common';
import DeviceDetector = require("device-detector-js");
#Injectable()
export class AppService {
private readonly deviceDetector = new DeviceDetector();
getHello(): string {
const userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81(Windows; Intel windows 8_8.1_10_11) Safari/537.36";
const result = this.deviceDetector.parse(userAgent);
console.log(JSON.stringify(result));
return 'Hello World!';
}
}
Output
[Nest] 23300 - 12/04/2022, 1:26:55 pm LOG [RouterExplorer] Mapped {/test, GET} route +2ms
[Nest] 23300 - 12/04/2022, 1:26:55 pm LOG [NestApplication] Nest application successfully started +4ms
{
host: 'localhost:3000',
connection: 'keep-alive',
'cache-control': 'max-age=0',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="98", "Google
Chrome";v="98"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
dnt: '1',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36'
}
It's working but not giving correct info as I am using Windows but it's showing Macintosh. Why is this happening?
Just pass headers from controller into service, something like this:
// controller
getHello(#Req() req): string {
console.log(req.headers);
return this.appService.getHello(req.headers);
}
// service
getHello(headers: {'user-agent': string }): string {
const userAgent = headers['user-agent'];
const result = this.deviceDetector.parse(userAgent);
console.log(JSON.stringify(result));
return 'Hello World!';
}

AWS-serverless-express never resolving with promises

can anyone shed light on my issue opened at the aws-serverless-express repo on github? https://github.com/awslabs/aws-serverless-express/issues/276
I am trying to run my previous express.js server using the aws-serverless-express package. When running without any special options I resolve, but the promises in the chain are never respected, meaning I dont execute all things in the event loop.
If I run the serverlessexpress with 'PROMISE' flag, I execute all my promises, but the program never resolves and times out after the maximum time set.
I even started a new project as per the example in that repo, same result.
My main executing file (index.js) when resolving but not respecting my promises
const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app.js')
const server = awsServerlessExpress.createServer(app, null)
exports.handler = (event, context) => {
return awsServerlessExpress.proxy(server, event, context)
}
My main executing file (index.js) when NOT resolving, but respecting my promises
const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app.js')
const server = awsServerlessExpress.createServer(app, null)
exports.handler = (event, context) => {
return awsServerlessExpress.proxy(server, event, context, 'PROMISE')
}
I also tried this:
const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app.js')
const server = awsServerlessExpress.createServer(app, null)
exports.handler = (event, context) => {
return awsServerlessExpress.proxy(server, event, context, 'PROMISE').promise
}
My express server file (app.js)
const express = require('express')
const bodyParser = require('body-parser')
const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware')
const app = express()
const router = express.Router()
router.use(bodyParser.json())
router.use(bodyParser.urlencoded({ extended: true }))
router.use(awsServerlessExpressMiddleware.eventContext())
router.get('/', (req, res) => {
res.render('index', {
apiUrl: req.apiGateway ? `https://${req.apiGateway.event.headers.Host}/${req.apiGateway.event.requestContext.stage}` : 'http://localhost:3000'
})
})
router.get('/users', (req, res) => {
res.json(users)
})
const users = [{
id: 1,
name: 'Joe'
}, {
id: 2,
name: 'Jane'
}]
function myFunc () {
console.log('hey')
}
setTimeout(myFunc, 3000)
app.use('/', router)
module.exports = app
Make sure you are setting context.callbackWaitsForEmptyEventLoop to false
read more about it here https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html
The issue was regarding my testing tool:
https://www.npmjs.com/package/run-local-lambda
When I spoke with the AWS Support, they told me to use:
sam local invoke "Test" -e event.json
With a event.json file that includes the following:
{
"httpMethod": "OPTIONS",
"//body": "{\"name\": \"Sam\"}",
"path": "/api/auth",
"resource": "/{proxy+}",
"queryStringParameters": {},
"pathParameters": {
"proxy": "users"
},
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, sdch, br",
"Accept-Language": "en-US,en;q=0.8",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-Country": "US",
"Content-Type": "application/json",
"Host": "xxxxxxxxxx.execute-api.us-east-1.amazonaws.com",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36",
"Via": "1.1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx_xxxxxxxxxxx_xxxx==",
"X-Forwarded-For": "11.111.111.111, 11.111.111.111",
"X-Forwarded-Port": "111",
"X-Forwarded-Proto": "http",
"x-apigateway-event": "hej"
},
"requestContext": {
"accountId": "111111111111",
"resourceId": "xxxxxx",
"stage": "prod",
"requestId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"identity": {
"cognitoIdentityPoolId": "",
"accountId": "",
"cognitoIdentityId": "",
"caller": "",
"apiKey": "",
"sourceIp": "11.111.111.111",
"cognitoAuthenticationType": "",
"cognitoAuthenticationProvider": "",
"userArn": "",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36",
"user": ""
},
"resourcePath": "/{proxy+}",
"httpMethod": "GET",
"apiId": "xxxxxxxxxx"
}
}
And a template file to use just for testing that looks like this:
AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description:
A test project
Resources:
Test:
Type: AWS::Serverless::Function
Properties:
Runtime: nodejs10.x
Handler: index.handler
Timeout: 10
Environment:
Variables:
NODE_ENV: "test"
DB_NAME: "dbname"
DB_USER: "dbuser"
DB_PASSWORD: "secret"
DB_URL: "dburl"
The code I use looks like this in the handler:
'use strict'
const awsServerlessExpress = require('aws-serverless-express')
const app = require('./main.js')
const server = awsServerlessExpress.createServer(app, null)
exports.handler = (event, context) => {
return awsServerlessExpress.proxy(server, event, context,'PROMISE').promise
}
Then my code executed wonderful with no timeouts ever.

nodejs http request post _http_outgoing type error

Environment: node: v4.5.0 os: windows 7 64bit.
Hi everyone, When I use http to post a request to a website, I received Error.
I have searched many answers and tryed these, but the error still persist. What could I do?
Here is error message:
_http_outgoing.js:351
throw new TypeError('The header content contains invalid characters');
TypeError: The header content contains invalid characters
at ClientRequest.OutgoingMessage.setHeader (_http_outgoing.js:351:13)
at new ClientRequest (_http_client.js:79:14)
at Object.exports.request (http.js:31:10)
at Object.<anonymous> (C:\Users\Administrator\http.js:30:16)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
Here is my code:
var http = require('http');
var querystring = require('querystring');
var postData = querystring.stringify({
'content': 'abc',
'mid': 8837
});
var options = {
hostname: 'www.imooc.com',
port: 80,
path: '/course/docomment',
method: 'POST',
headers: {
'Accept':'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding':'gzip, deflate',
'Accept-Language':'en,zh-CN;q=0.8,zh;q=0.6',
'Connection':'keep-alive',
'Content-Length':postData.length,
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
'Cookie':'imooc_uuid=e9896052-b2fb-4b23-85e8-74673c7ac165; imooc_isnew_ct=1476177989; loginstate=1; apsid=lhNTQ3NDk4OWFjZDBiNTFmODBjMmNkNDRmY2NjMmUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMjEzMzI0NAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAxMDI3MTkxNDkzQHFxLmNvbQAAAAAAAAAAAAAAAAAAADY1NTdiODMyZmM5NDMzZWNjYzg2ZGRjNDc5YzU3NTA2VrD8V1aw%2FFc%3DNz; last_login_username=1027191493%40qq.com; PHPSESSID=0ib2e90urbk47po8su89tvks13; IMCDNS=0; jwplayer.qualityLabel=è?????; jwplayer.volume=100; Hm_lvt_f0cfcccd7b1393990c78efdeebff3968=1476346029,1476363725,1476416065,1476448867; Hm_lpvt_f0cfcccd7b1393990c78efdeebff3968=1476459900; imooc_isnew=2; cvde=5800d25c99f68-52',
'Host':'www.imooc.com',
'Origin':'http://www.imooc.com',
'Referer':'http://www.imooc.com/video/8837',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36',
'X-Requested-With':'XMLHttpRequest'
}
}
var req = http.request(options, function(res) {
console.log('status:' + res.statusCode);
console.log('headers:' + JSON.stringify(res.headers));
res.on('data', function(chunk) {
console.log(Buffer.isBuffer(chunk));
console.log(typeof chunk);
});
res.on('end', function() {
console.log('评论完毕');
});
res.on('error', function(e) {
console.log('error:' + e.message);
});
})
req.write(postData);
req.end();
Thanks in advance!!!

Socket.io handshake address value changes

This is peculiar. Socket.io version ~1.3
io.sockets.on('connection', function (socket) {
console.log('Client connected from: ' + socket.handshake.address);
}
Returns
Client connected from: ::1
However
io.sockets.on('connection', function (socket) {
console.log(socket.handshake);
console.log('Client connected from: ' + socket.handshake.address);
}
Returns
{ headers:
{ host: 'localhost:8000',
connection: 'keep-alive',
origin: 'http://localhost:3000',
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTM
L, like Gecko) Chrome/43.0.2357.130 Safari/537.36',
accept: '*/*',
dnt: '1',
referer: 'http://localhost:3000/dev.html',
'accept-encoding': 'gzip, deflate, sdch',
'accept-language': 'en-US;q=0.8,en;q=0.6,ko;q=0.4,de;q=0.2,ru;q=0.2,fr;q=0.2,ja;q=0.2,it;q=0.2',
cookie: 'io=yhyuAabou3GufhzNAAAA' },
time: 'Wed Jun 24 2015 22:50:19 GMT+0200 (Central European Daylight Time)',
address: '::ffff:127.0.0.1',
xdomain: true,
secure: false,
issued: 1435179019584,
url: '/socket.io/?EIO=3&transport=polling&t=1435179017804-3',
query: { EIO: '3', transport: 'polling', t: '1435179017804-3' } }
Client connected from: ::ffff:127.0.0.1
Why? Is there some ES6 proxy in the way? I thought maybe some weird JS conversion magic was in place, but it doesn't seem like it.
::ffff:127.0.0.1 is an IPv6 version of 127.0.0.1 and ::1 is an IPv6 shortcut for both.
See Express.js req.ip is returning ::ffff:127.0.0.1 for a similar question.

Resources