nodejs – Error in the REST node API

I'm trying to request a listing of my properties, but when I do, I get an error 500 and it tells me exactly the following: "CastError: Cast to ObjectId for value" mine "failed on path" _id "for model" Property " , The problem is that the ID does not take it well, but I do not know why, it should not Enter the description of the picture here

node.js – NodeJS cluster, useful use cases?

I'm researching use cases to introduce cluster computing with nodejs to a few friends.

My goal is to explain some real cases of nodejs clusters, and why knotjs is helpful in creating these special cases.

So far, I really like a single case:

  • Clustering a web server (like an API server to REST) (Reason: load distribution)

I have seen examples of expensive computation and the advantages that arise on a different thread / processor.

This has led me to think about whether there are work pools where each pool performs different parts, e.g. For example, connecting to a database, generating PDFs, and the like.

While trying to understand the connections between the worker and the master, I played around and am still unsure:

  • nodejs clusters are only suitable for running the same process in parallel (documentation suggests)
  • or does it make sense to have node cluster clusters as separate tasks in pools.

.. which leads to the following results: Which use cases are actually real with nodejs?

I hope it is understandable.
Opinions are very welcomed.

node.js – How do I access the user every time I ask the Google Assistant in NodeJS?

const express = require (& # 39; express & # 39;);
const bodyParser = required (& # 39; body parser & # 39;);


// Import the appropriate service and the selected wrappers
const {
conversational flow,
Image,
Basic Card,
Button,
} = required (& # 39; actions-on-google & # 39;);

// Create an app instance
const app = dialogflow ();



// Register handlers for dialogflow intents

app.intent (& # 39; standard welcome intent & # 39 ;, conv => {
conv.ask (& # 39; Hi, how are you? & # 39;);
conv.ask (& # 39; s an image of a cat & # 39;);
conv.ask (new image ({
URL: "https://developers.google.com/web/fundamentals/accessibility/semantics-builtin/imgs/160204193356-01-cat-500.jpg",
old: "A cat",
}));
});


const expressApp = express (). use (bodyParser.json ());

expressApp.post (& # 39; / fulfillment & # 39 ;, app);

expressApp.listen (process.env.PORT || 3000, function () {
console.log ("Listen and listen server");
});

javascript – Node.js http retry do while mechanism

I've implemented a simple do / while loop to handle an http request to a third party. I do not know when the data I got has elements in its array, so I try it up to ten times until the array given to me contains data that is then paused and stored in my database.

My question is in terms of performance. Is my API influenced by this iteration mechanism with a simple do / while loop? If so, what's the better way to implement a repeat mechanism like this?

public MAX_RETRY = 10;

public async processMetrics (sessionId: any, side: any): Promise  {

To attempt {

leave metrics;
let retryAttempt = 0;

do {
Expect new promise (done) => setTimeout (done, 2000));
Metrics = Expect these.getMetrics (session._id, sessionRequest._id);
retryAttempt ++;

} while (! metrics.body.metrics.length && (retryAttempt <this.MAX_RETRY));

// save to DB
} catch (err) {


}

}

public async getMetrics (sessionId: any, requestId: any): Promise  {

const url = this.config.backendUrl + "/ check / metrics";

const options = {
uri: url,
Headers: {
"X-IDCHECK-SESSION_ID": sessionId,
},
Body: {},
json: true,
resolWithFullResponse: true,
};

const metrics = await request.get (options);

Return values;

}

I call processMetrics from a different asynchronous function. processMetrics calls a backend (getMetrics) every 2 seconds, repeating it ten times to see if the result is ready. If so, I will save something in the database and then return.

node.js – Protection of node / express endpoints against DDoS attacks by delay with reset

I want to protect some endpoints from DoS / DDoS attacks, such as: Login, reset password, register.

I want to implement a delay with linear or exponential backoff (not throttling). So:

  • Please 1: no delay
  • Requirement 2: 1000ms
  • Requirement 3: 1000ms + x
  • Requirement 4: 1000 ms + x + y

I've done that before, but not in the knot.

I know that the attacker can not starve my thread pool by starting 1000 concurrent requests because there is no thread pool in the node.

However, each request requires a small amount of memory. The attacker could launch thousands of concurrent requests and exhaust my server's memory.

Is my understanding right? Is there an elegant way to handle it or is it inevitable (and therefore I should accept it or consider an alternative such as throttling).

node.js – How to count the elements and paginate the query result in Loopback 4?

I have the following repository:

import {DefaultCrudRepository} from & # 39; @ loopback / repository & # 39 ;;
{User} from & # 39; ../models & # 39; import;
import {DbDataSource} from & # 39; ../ datasources & # 39 ;;
{Injection} off & # 39;; @ loopback / core & # 39; import;

The export class UserRepository extends DefaultCrudRepository {
builder
@inject (& # 39; datasources.db & # 39;) dataSource: DbDataSource,
) {
great (user, data source);
}
}

On my controller, when I run this.userRepository.find () All users registered in my database will be returned. However, I have to return this result. I have passed the options limit and offset for the method find () but I need the total number of users found.

linux – Very slow reverse proxy Node.js & Angular on Nginx

I have nginx keep running Ubuntu 16.04.5 LTS GNU / Linux 4.4.0-137 generic x86_64.

Work as a Reverse proxy to the nodejs app and handling angular 6 AppIt worked fine, but after the reverse proxy for the node js was created and installed SSL For ports, processing a request takes 10-15 seconds on average.

If someone can help me find out the reason for this slow behavior

This is my nginx.conf File:

worker_processes 4;
pid /var/run/nginx.pid;

error_log /var/log/nginx.error_log info;

Events {Events
worker_connections 8096;
multi_accept on;
use epoll;
}

worker_rlimit_nofile 65535;

http {
belong to mime.types;
default_type application / octet-stream;

log_format main & # 39; $ remote_addr - $ remote_user [$time_local] & # 39;
& # 39; "$ request" $ status $ bytes_sent & # 39;
"$ http_referer" "$ http_user_agent" "
& # 39; "$ gzip_ratio" & # 39 ;;

log_format download & # 39; $ remote_addr - $ remote_user [$time_local] & # 39;
& # 39; "$ request" $ status $ bytes_sent & # 39;
"$ http_referer" "$ http_user_agent" "
& # 39; "$ http_range" "$ send_http_content_range" & # 39 ;;

client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;

client_header_buffer_size 1k;
large_client_header_buffers 4 4k;

gzip on;
gzip_disable "msie6";

gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types text / plain text / css-application / json application / x-javascript
Text / XML Application / XML Application / XML + RSS Text / Javascript
application / vnd.ms-fontobject application / x-font-ttf-font / opentype
image / svg + xml image / x-icon;

Output buffer 1 32k;
postpone_output 1460;

sendfile on;
tcp_nopush;

tcp_nodelay;

keepalive_timeout 75 20;

Server {
if ($ host = my-domain) {
return 301 https: // $ host $ request_uri;
}

if ($ host = subdomain) {
return 301 https: // $ host $ request_uri;
}

listen 80;
server name my-domain;
root / var / www / html;
access_log /var/log/nginx.access_log main;

Location / {
try_files $ uri /index.html;
}
}

Server {
listen 443;
ssl on;
ssl_certificate /etc/letsencrypt/live/my-domain/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/my-domain/privkey.pem;
server name my-domain;
root / var / www / html;
ssl_session_timeout 5m;
ssl_session_cache shared: SSL: 5m;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers & # 39; cipher text .. & # 39 ;;
ssl_prefer_server_ciphers on;

server_tokens off;
access_log /var/log/nginx.access_log main;

Location / {
try_files $ uri /index.html;
}

location / server_proxy / {
proxy_pass http: // localhost: 3000 /;
proxy_http_version 1.1;
proxy_set_header Upgrade $ http_upgrade;
proxy_set_header connection & # 39; upgrade & # 39 ;;
proxy_set_header Host $ host;
proxy_cache_bypass $ http_upgrade;
}
}
}

Beginner – Batch script for downloading and extracting Node.JS and NPM packages

The following is my first real attempt at batch scripting. I'm new to it, so I'm not sure if I want to apply best practice. The code works and I am open to suggestions.

Basically, the following code allows you to download Node.js and npm per project. This is a requirement for us to be able to run multiple versions of Node and npm simultaneously. The script also means that setting up development environments is easier. So there are some goals this script should achieve.

@ ECHO OFF

REM must be tested on Azure DevOps, CruiseControl.NET, and so on.
REM test with spaces in paths ...

SETLOCAL

SET "NPM_REGISTRY = https: //registry.npmjs.org"
SET "ZIP_DOWNLOAD_URL = https: //chocolatey.org/7za.exe"

SET "NODE_EXE_ARCH = x64"
SET "NODE_PACKAGE_VERSION = 11.10.0"
SET "NPM_PACKAGE_VERSION = 6.8.0"

SET "DOWNLOAD_CACHE_PATH =% temp%  NpmScratch"
SET "ZIP_EXE_DOWNLOAD =% DOWNLOAD_CACHE_PATH%  7za.exe"

SET "NODE_PACKAGE_PATH =% ~ dp0."
SET "NODE_MODULES_PATH =% NODE_PACKAGE_PATH%  node_modules"
SET "NPM_PACKAGE_PATH =% NODE_MODULES_PATH%  npm"

SET "NODE_EXE =% NODE_PACKAGE_PATH%  node.exe"
SET "NPM_CLI_JS =% NPM_PACKAGE_PATH%  bin  npm-cli.js"

SET "DELETE_NODE_MODULES ="
SET "DOWNLOAD_NODE ="
SET "DOWNLOAD_NPM ="

IF NOT EXIST "% NODE_EXE%" (
SET DOWNLOAD_NODE = 1
)

IF NOT EXIST "% NPM_CLI_JS%" (
SET DELETE_NODE_MODULES = 1
SET DOWNLOAD_NPM = 1
)

ECHO DELETE_NODE_MODULES =% DELETE_NODE_MODULES%
ECHO DOWNLOAD_NODE =% DOWNLOAD_NODE%
ECHO DOWNLOAD_NPM =% DOWNLOAD_NPM%

IF DEFINED DELETE_NODE_MODULES (
CALL: clean_dir% NODE_MODULES_PATH%
)

SET "NODE_PACKAGE_FILENAME = node-win-% NODE_EXE_ARCH% -% NODE_PACKAGE_VERSION% .tgz"
SET "NODE_PACKAGE_DOWNLOAD_PATH =% DOWNLOAD_CACHE_PATH% % NODE_PACKAGE_FILENAME%"
SET "NODE_DOWNLOAD_CACHE_PATH =% DOWNLOAD_CACHE_PATH%  nodejs-% NODE_PACKAGE_VERSION%"
SET "NODE_EXE_DOWNLOAD_CACHE_PATH =% NODE_DOWNLOAD_CACHE_PATH%  bin  node.exe"
IF DEFINED DOWNLOAD_NODE (
CALL: create_dir% DOWNLOAD_CACHE_PATH%
IF NOT EXIST "% DOWNLOAD_CACHE_PATH%" (
ECHO "cache directory is missing"
EXIT% ERRORLEVEL%
)

CALL: download% ZIP_DOWNLOAD_URL %% ZIP_EXE_DOWNLOAD%
IF NOT EXIST "% ZIP_EXE_DOWNLOAD%" (
ECHO "Zip download failed"
EXIT% ERRORLEVEL%
)

CALL: download% NPM_REGISTRY% / node-win-% NODE_EXE_ARCH% / - /% NODE_PACKAGE_FILENAME %% NODE_PACKAGE_DOWNLOAD_PATH%
IF NOT EXIST "% NODE_PACKAGE_DOWNLOAD_PATH%" (
ECHO "Node.js download failed"
EXIT% ERRORLEVEL%
)

IF NOT EXIST "% NODE_EXE_DOWNLOAD_CACHE_PATH%" (
CALL: extract_archive% ZIP_EXE_DOWNLOAD %% NODE_PACKAGE_DOWNLOAD_PATH %% DOWNLOAD_CACHE_PATH%
IF NOT EXIST "% DOWNLOAD_CACHE_PATH%  package  bin  node.exe" (
ECHO "extract nodes, js package failed"
EXIT% ERRORLEVEL%
)

CALL: delete_dir% NODE_DOWNLOAD_CACHE_PATH%
IF EXIST "% NODE_DOWNLOAD_CACHE_PATH%" (
"ECHO"% NODE_DOWNLOAD_CACHE_PATH% could not be deleted "
EXIT% ERRORLEVEL%
)

CALL:% DOWNLOAD_CACHE_PATH%  package% NODE_DOWNLOAD_CACHE_PATH% Move
IF NOT EXIST "% NODE_EXE_DOWNLOAD_CACHE_PATH%" (
ECHO "Renaming Node.js Package Directory Failed"
EXIT% ERRORLEVEL%
)
)

IF NOT EXIST "% NODE_EXE_DOWNLOAD_CACHE_PATH%" (
ECHO "Cached Node.js Package Directory Is Missing"
EXIT% ERRORLEVEL%
)

CALL: delete_file% NODE_EXE%
IF EXIST "% NODE_EXE%" (
ECHO "% NODE_EXE% could not be deleted"
EXIT% ERRORLEVEL%
)

CALL: copy_file% DOWNLOAD_CACHE_PATH%  nodejs-% NODE_PACKAGE_VERSION%  bin  node.exe% NODE_EXE%
IF NOT EXIST "% NODE_EXE%" (
ECHO "Failed to copy node text"
EXIT% ERRORLEVEL%
)
)

SET "NPM_PACKAGE_FILENAME = npm-% NPM_PACKAGE_VERSION% .tgz"
SET "NPM_PACKAGE_DOWNLOAD_PATH =% DOWNLOAD_CACHE_PATH% % NPM_PACKAGE_FILENAME%"
SET "NPM_CLI_JS_DOWNLOAD_CACHE_PATH =% DOWNLOAD_CACHE_PATH%  npm-% NPM_PACKAGE_VERSION%  bin  npm-cli.js"
IF DEFINED DOWNLOAD_NPM (
CALL: create_dir% DOWNLOAD_CACHE_PATH%
IF NOT EXIST "% DOWNLOAD_CACHE_PATH%" (
ECHO "cache directory is missing"
EXIT% ERRORLEVEL%
)

CALL: download% ZIP_DOWNLOAD_URL %% ZIP_EXE_DOWNLOAD%
IF NOT EXIST "% ZIP_EXE_DOWNLOAD%" (
ECHO "Zip download failed"
EXIT% ERRORLEVEL%
)

Download CALL:% NPM_REGISTRY% / npm / - /% NPM_PACKAGE_FILENAME %% NPM_PACKAGE_DOWNLOAD_PATH%
IF NOT EXIST "% NPM_PACKAGE_DOWNLOAD_PATH%" (
ECHO "npm download failed"
EXIT% ERRORLEVEL%
)

CALL: extract_archive% ZIP_EXE_DOWNLOAD %% NPM_PACKAGE_DOWNLOAD_PATH %% NPM_PACKAGE_PATH%  ..
IF NOT EXIST "% NODE_MODULES_PATH%  package  bin  npm-cli.js" (
ECHO "npm package could not be extracted"
EXIT% ERRORLEVEL%
)

CALL: delete_dir% NPM_PACKAGE_PATH%
IF EXIST "% NPM_PACKAGE_PATH%" (
ECHO "% NPM_PACKAGE_PATH% could not be deleted"
EXIT% ERRORLEVEL%
)

CALL:% NPM_PACKAGE_PATH%  ..  Move Package% NPM_PACKAGE_PATH%
IF NOT EXIST "% NPM_PACKAGE_PATH%" (
ECHO "Renaming the npm package directory failed"
EXIT% ERRORLEVEL%
)
)

EXIT / B% ERRORLEVEL%

: clean_dir
IF EXIST "% ~ 1" (
ECHO "Cleaning Directory% ~ 1"
CALL CMD / C "MKDIR% temp% . Empty 2> NUL"
CALL CMD / C "ROBOCOPY% temp% . Empty% ~ 1 / MIR> NUL"
)
EXIT / B 0

:Delete file
IF EXIST "% ~ 1" (
CALL ECHO "Deleting the file% ~ 1"
CALL DEL "% ~ 1"
)
EXIT / B 0

: delete_dir
IF EXIST "% ~ 1" (
ECHO "delete directory% ~ 1"
CALL: clean_dir "% ~ 1"> NUL
CALL RMDIR "% ~ 1"
)
EXIT / B 0

: create_dir
If not "% ~ 1" exists (
ECHO "create directory% ~ 1"
Call MKDIR "% ~ 1"
)
EXIT / B 0

:Move
IF NOT "% ~ 2" (
Move ECHO "% ~ 1 to% 2"
CALL MOVE "% ~ 1" "% ~ 2"
)
EXIT / B 0

: Copy file
IF NOT "% ~ 2" (
ECHO "Copy from% ~ 1 to% ~ 2"
CALL COPY% ~ 1 "% ~ 2"
)
EXIT / B 0

: copy_dir
IF NOT "% ~ 2" (
ECHO "Copy from% ~ 1 to% 2"
CALL Robocopy "% ~ 1" "% ~ 2" / E> NUL
)
EXIT / B 0

download
IF NOT "% ~ 2" (
Download ECHO "% ~ 1 to% ~ 2"
CALL bitsadmin.exe / transfer "Download"% ~ 1 "% ~ 2"
)
EXIT / B 0

REM% ~ 1 =% ZIP_EXE_DOWNLOAD%
REM% ~ 2 =% NODE_PACKAGE_DOWNLOAD_PATH%
REM% ~ 3 =% DOWNLOAD_CACHE_PATH%
: extract_archive
ECHO "Extract from% ~ 2 to% ~ 3"
CALL CMD / C% ~ 1 x% ~ 2 -so |% ~ 1 x -aoa -si-tar -o% ~ 3
EXIT / B 0

node.js – Query instead of active sockets

I plan to create a web application in Node.js that serves content for an app. The app should deliver comparable posts with a Facebook post and offer a chat and comment feature. I had some design ideas based on different articles in different forums, but I have too little experience to know what the will is and what does not work. Okay, here are the questions:

Is it a viable option for the client to query the server for updates (not polling for long) instead of maintaining an active connection (socket.io)? The node.js server would then use caching methods to check if an update is required before initiating a db / other connection. The actions of other users then cause other users to retrieve changes.

Would this method work in production? It is important to lower the cost of the servers while supporting scaling to a relatively large number of users. Active socket connections do not scale well to my knowledge. In addition, adequate latency is important because of the chat / comment feature.

Can the cache solution be scaled well with many servers in all regions (using nginx as a load balancer)? Should Redis be used for syncing, and would that be too expensive for performance reasons?

Thanks in advance for your help.