Articles under category Nodejs

If there is novice learning Nodejs Get URL post When the parameters are returned, the same problem will happen to me, and today we will solve this problem.

Node.js Unlike PHP This old backend language has a specific way to obtain external parameters, after all, it is a new force, hoping that the later version will be more convenient and efficient.


 / / normal acquisition value 
app.get ("/", (req, RES) = {
console.log (req.query); 
res.send ("finish");}; 
app.post ("/", (req, RES) = > {
console.log (req.body); 
res.send ("finish");};

If you want to export normally, your anticipation is like this.

 / / GET request 
{
name: 'Feng Xiaoxian', 
age:'18'
}
 complete.

or

 / / POST request 
{
name: 'Feng Xiaoxian', 
age:'18'
}
 complete.

However, it is not!!!

 / / the two output will be like this!!! {}
undefined

This is because we lack a module, express middleware. Body-parser

Official statement
Node.js body parsing middleware.
Parse incoming request bodies in a middleware before your handlers, available, handlers

Basically, parsing the content of the body in the client request, JSON encoding and URL encoding processing.


Next we will install. Body-parser

 / / install body-parser
npm install body-parser

 / / import app.js
var bodyParser = require ('body-parser'); 
app.use (bodyParser); / so / req.body, to use req.body to get the value of post transfer, 
app.post ("/", (req, RES) = > 
app.post ("));" ("completion");};

Learning without motivation, eating jujube is? In the process of learning nodejs, I will give you a little refreshing.

Here I use the cnodejs community's simplest crawler tutorial to do the basic modification of eel.

 / / the magic module 
require ('superagent') used in site environment 
node v6.10.2
npm v3.10.10
//; 
require ('cheerio'); 
require ('express'); 
require ('ejs');'express'(E); (()); (());

Initialization project
NPM init
Installation module use
NPM install module name --save
Start app.js
Node app.js
You can play happily (the download link is at the end of the article).

If you don't know how to use nodejs
Linux players can see My article Windows players can download.Msi directly to the official website, and install next step. Through train


Basic crawling ideas

  1. The paging button of the page catches all the little sister's page links appearing on this page and stores them.
  2. Using eventproxy to visit every little sister's page concurrently
  3. During the visit, we use cheerio (similar to jQuery module) to match the picture address we need, and record all the pictures found.
  4. HTTP requests every picture address to save binary data stream.
  5. FS outputs binary data streams into picture files, and the name of the files is extracted from the original address.
  6. Save to folder

Here, I am afraid that because the request is too large, we can use express to build static pages. query and download To reduce the possibility of crawling sites being crawled to death.

Download address: Xjj.zip

Foundation: installation

server information :
1.System

CentOS release 6.9 (Final)
Kernel r on an M

2.Configuration

Intel (R) Xeon (R) CPU E5-2650 V2 @ 2.60GHz
1G, 1 core (ALI lackey cloud)

Step 1

Here I use my big Taobao source.

 When you enter the storage directory, I put it under /home/nodejs, where do I see you like spicy 
cd /home/nodejs
# download nodejs binary package 
wget Https://npm.taobao.org/mirrors/node/v6.10.2/node-v6.10.2-linux-x86.tar.gz 
tar -xzvf node-v6.10.2-linux-x86.tar.gz
# extract a folder node-v6.10.2-linux-x86, enter the directory bin directory 
cd node-v6.10.2-linux-x86/bin
# confirm the download nodejs version is correct 
./node -v

Step 2

Now you can only operate node in this directory, so we have to add it to the user environment.

 Look at the directory where 
pwd
# is currently added, add a soft connection to /usr/local/bin
ln -s /home/nodejs/node-v6.10.2-linux-x86/bin/node /usr/local/bin/node
ln -s /home/nodejs/node-v6.10.2-linux-x86/bin/npm /usr/local/bin/npm



- reading the remainder part -