如何使用Node.js测试文件

问题描述 投票:0回答:1

这是我第一次使用Node.js和Express。我会创建一个网络抓取。这是我的项目结构:

WebScrape:
  |_ bin
  |_ node_modules
  |_ public
  |_ routes
  |_ view
  |_ app.js
  |_ package.js
  |_ package-lock.json

我在scrape.js目录中创建了一个routes文件:

var express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var app = express();

app.get('/scrape', function(req, res) {

    // the URL we will scrape from - in our example Anchorman 2
    url = 'http://www.imdb.com/title/tt1229340/';

    /**
     * The structure of our request call.
     * The first parameter is our URL.
     * The callback function takes 3 parameters: an error, a response status code and the html.
     */
    request(url, function(error, response, html) {

        // check to make sure no errors occurred when making the request
        if(!error) {
            // utilize the cheerio library on the returned html which will essentially give us jQuery functionality
            var $ = cheerio.load(html);

            // finally, we'll define the variables we're going to capture
            var title, release, rating;
            var json = { title : "", release : "", rating : ""};
        }
    }) // end request
}) // end get

app.listen('8081')
console.log('Magic happens on port 8081');

exports = module.exports = app;

我该怎么测试呢?这是正确的地方吗?

node.js express web-scraping
1个回答
0
投票
var express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var router = express.Router();

router.get('/scrape', function(req, res) {

    // the URL we will scrape from - in our example Anchorman 2
    url = 'http://www.imdb.com/title/tt1229340/';

    /**
     * The structure of our request call.
     * The first parameter is our URL.
     * The callback function takes 3 parameters: an error, a response status code and the html.
     */
    request(url, function(error, response, html) {

        // check to make sure no errors occurred when making the request
        if(!error) {
            // utilize the cheerio library on the returned html which will essentially give us jQuery functionality
            var $ = cheerio.load(html);

            // finally, we'll define the variables we're going to capture
            var title, release, rating;
            var json = { title : "", release : "", rating : ""};
        }
    }) // end request
}) // end get

exports = module.exports = router;

通常,app.js会在端口上侦听请求。您可以使用express.Router在单独的路由器文件中进一步扩展和添加路由。

app.js你必须这样做才能真正添加​​路线:

const routes = require('./routes/scraper.js');

// app is the express() app
app.use(routes);
© www.soinside.com 2019 - 2024. All rights reserved.