【问题标题】:How to test a file using Node.js如何使用 Node.js 测试文件
【发布时间】:2018-03-05 12:58:29
【问题描述】:

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

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

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

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】:
    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);
    

    【讨论】:

      猜你喜欢
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 2017-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多