Tag Archives: node.js

egg.js The frame post request reported an error of invalid CSRF token security verification, which has been solved

It has to be said that Alibaba’s egg framework is quite good, with its own security verification.

Problem: get request is normal, post request background will report such an error.

" nodejs.ForbiddenError : invalid csrf token"

There is an official explanation for this problem. Click to jump to the official safety explanation of egg. There is not too much explanation here

Method 1: in the confit.default.js Add the following code to turn off security verification (not recommended)

config.security = {
    csrf: {
      enable: false,
    },
  };

Method 2: when the front-end initializes the interface, let the front-end get to request an interface first, and the background returns a secret key to the front-end. Let the front-end put it in the headers request header when the post request is made, and the egg will automatically verify the secret key, and the request will succeed only if the verification is successful.

1. Egg background code, get interface returns secret key: 2

async index() {
    const { ctx } = this;
    ctx.body = {
      csrf:ctx.csrf
    };
}

The following two methods are demonstrated. The postman test is as follows. At the same time, if a request is made in the front end, a secret key pair will be generated in the cookies. As shown in the figure below, the secret key will change after each request, so the CSRF obtained in the front end should be put in the headers request header in a global way.

2. Front end secret key request: 2

axios.post('apis/add', data,{headers:{'x-csrf-token': headData}})

The postman test is as follows: directly copy CSRF

OK, successful request, perfect solution, start moving bricks

Gyp err! Build error stack error

Error content:

gyp ERR! build error
gyp ERR! stack Error: `C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\BuildTools\\MSBuild\\15.0\\Bin\\MSBuild.exe` failed with exit code: 1

Repeat several times and still report an error.
Check the problem. The yarn version is normal, and the image is also a Taobao image.

solve:

1. Execute NPMI – G node gyp command
2. Delete directory/node_ All files under modules
3. Re install.

In addition, it can be seen on the Internet that it may be due to the node version. The node version needs to be lowered. I didn’t try this method. I don’t know if it’s feasible.

Error: EBUSY: resource busy or locked

Error: EBUSY: resource busy or locked, LSTAT ‘C:\ swapfile.sys ’。
Here are some effective methods I summarized:

Method 1: delete node_ Module, and then install (NPMI);
method 2: delete node_ Modules, run NPM cache clean or NPM cache clean — force command, and then install;
method 3: upgrade the node version and delete the node_ Modules, and then install it.

NPM run dev error: cannot find module ‘webpack cli / bin / config yargs‘

1- Open your package.json file

"devDependencies": {
    "webpack": "^5.2.0",
    "webpack-cli": "^4.1.0",
    "webpack-dev-server": "^3.11.0"
}

Webpack-cli: “^4.1.0”, which is version 4.*
2- Uninstall the current webpack-cli directive: NPM uninstall webpack-cli
3.* version directive: NPM install webpack-cli@3 -d

Just run NPM run dev!
 

When NPM install, make: G + +: command not found will be prompted

When NPM install is installed, an error may appear that prompts make:g++ Command not found, as follows:
node-pre-gyp WARN Using request for node-pre-gyp https download
node-pre-gyp WARN Tried to download(404): https://github.com/yanyiwu/nodejieba/releases/download/2.4.1/nodejieba-v2.4.1-node-v72-linux-x64.tar.gz
node – pre – gyp WARN the pre – built binaries not found for [email protected] and [email protected] (node-v72 ABI, glibc) (falling back to source compile with node-gyp)
make: Entering directory /home/smart/jenkins/workspace/microservice_vue_demo/node_modules/nodejieba/build' CXX(target) Release/obj.target/nodejieba/lib/index.o make: g++: Command not found make: * * * [Release/obj. Target/nodejieba/lib/index o] make the Error 127: brigade directory /home/smart/Jenkins/workspace/microservice_vue_demo/node_modules/nodejieba/build '
gyp ERR! build error
gyp ERR! stack Error: make failed with exit code: 2
gyp ERR! stack at ChildProcess.onExit (/usr/local/nodejs/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:194:23)
gyp ERR! stack at ChildProcess.emit (events.js:310:20)
gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
gyp ERR! System Linux 3.10.0-957.21.3. El7.x86_64 The command "/ usr/local/nodejs/bin/node" "/ usr/local/nodejs/lib/node_modules/NPM/node_modules/node - gyp/bin/node - gyp. Js" "build" fallback "- - to - build" "- the module =/home/smart/Jenkins/workspace/microservice_vue_demo/node_modules/nodejieba/build/Release/nodejieba node" "- module_name = nodejieba" "- module_path =/home/smart/Jenkins/workspace/microservice_vue_demo/node_modules/nodejieba/build/Release" "- napi_version = 5" "- node_abi_napi = for a" "- napi_build_version = 0" "- node_napi_label = node - v72"
gyp ERR! cwd /home/smart/jenkins/workspace/microservice_vue_demo/node_modules/nodejieba
gyp ERR! Node-v v12.16.3 Node-gyp-v v5.1.0
gyp ERR! not ok
node-pre-gyp ERR! build error
node-pre-gyp ERR! stack Error: Failed to execute '/ usr/local/nodejs/bin/node/usr/local/nodejs/lib/node_modules/NPM/node_modules/node - gyp/bin/node - gyp. Js build - fallback - to - build --module=/home/smart/jenkins/workspace/microservice_vue_demo/node_modules/nodejieba/build/Release/nodejieba.node --module_name=nodejieba - module_path =/home/smart/Jenkins/workspace/microservice_vue_demo/node_modules/nodejieba/build/Release - napi_version = 5 - node_abi_napi = for a - napi_build_version = 0 - node_napi_label = node - v72 (1)
' node-pre-gyp ERR! stack at ChildProcess. (/home/smart/jenkins/workspace/microservice_vue_demo/node_modules/node-pre-gyp/lib/util/compile.js:83:29)
node-pre-gyp ERR! stack at ChildProcess.emit (events.js:310:20)
node-pre-gyp ERR! stack at maybeClose (internal/child_process.js:1021:16)

install g++, switch to root, and execute

root@linx:~# yum install gcc -c++

After waiting for the package installation to complete, execute g++ -v to see that g++ has been successfully installed

root@linx:~# g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 

Re-run NPM Install.

node.js Error: write epipe problem solved!

1. Open the Gulpfile.js file in the installation directory
2, as shown in the figure below, the error place annotation or default in the operation will be deleted accordingly. If you need to use the annotation, please download the lower version and try to install the slave again


3. Error report is shown in the figure below

Nodejs exception error: listen eacces 127.0.0.1:8000

This article is reprinted, if you do not want to infringe copyright, please timely inform
The original address to http://blog.csdn.net/y2010081134/article/details/72800812
Exception information

Error: listen EACCES 127.0.0.1:8000
    at Object.exports._errnoException (util.js:1018:11)
    at exports._exceptionWithHostPort (util.js:1041:20)
    at Server._listen2 (net.js:1245:19)
    at listen (net.js:1294:10)
    at net.js:1404:9
    at GetAddrInfoReqWrap.asyncCallback [as callback] (dns.js:62:16)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:81:10)

npm ERR! Windows_NT 10.0.14393
npm ERR! argv "D:\\nodejs\\node.exe" "D:\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! node v6.10.3
npm ERR! npm  v3.10.10
npm ERR! code ELIFECYCLE
npm ERR! [email protected] start: `http-server ./app -a localhost -p 8000 -c-1`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script 'http-server ./app -a localhost -p 8000 -c-1'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the angular-phonecat package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     http-server ./app -a localhost -p 8000 -c-1
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs angular-phonecat
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls angular-phonecat
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     E:\Git\Repository\angular-phonecat\npm-debug.log

The solution


The exception is caused by 8000 port being occupied. Check the 8000 port being occupied under Windows

E:\Git\Repository\angular-phonecat>netstat -ano|findstr "8000"
  TCP    0.0.0.0:8000           0.0.0.0:0              LISTENING       18480
  TCP    127.0.0.1:8000         127.0.0.1:50236        TIME_WAIT       0
  TCP    127.0.0.1:8000         127.0.0.1:50237        TIME_WAIT       0
  TCP    127.0.0.1:8000         127.0.0.1:50238        TIME_WAIT       0
  TCP    127.0.0.1:8000         127.0.0.1:50239        TIME_WAIT       0
  TCP    127.0.0.1:8000         127.0.0.1:50240        TIME_WAIT       0
  TCP    127.0.0.1:8000         127.0.0.1:50286        TIME_WAIT       0
  TCP    127.0.0.1:50285        127.0.0.1:8000         TIME_WAIT       0
  UDP    0.0.0.0:8000           *:*                                    18480

Because process 0 is a system process, executing “Taskkill/PID 0” will not kill it. So, find another way to change the port, as follows:

Under node’s installation root, find package.json, search 8000, and replace with 8888


 

Then execute the command NPM Start from the command line, and it starts successfully

Enter it in the browser
http://localhost:8888
, you can see the successful access