문제

Im trying to build an basic parcer using Node/Express and Cheerio but while server is running okay, i receive no page in browser. Here's the code of server.js:

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

app.engine('.html', require('ejs').__express);
app.engine('html', require('ejs').renderFile);
app.set('views', __dirname + '/views');

app.get('/main', function(req, res) {
    url = 'http://tres-bien.com/new-arrivals/';
    request(url, function(error, response, html) {

    var title, price, image;
    var tres_bien = [ { title : 'tres'}, { price : 'bien'} ];
    var images = [];

        if (!error) {
            var $ = cheerio.load(html);
            $(".grid-info").each(function() {
                var data = $(this);
                title = data.children().first().text().replace(/(\r\n|\n|\r|\t)/gm, "");
                price = data.children().last().children().text().replace(/(\r\n|\n|\r|\t|\s)/gm, "");
            });
            $(".dropjaw img").each(function() {
                image = this.attr('href');
            });
        }   
    }); 
});// APP GET

app.listen('8081')
exports = module.exports = app;

Thank you in advance.

도움이 되었습니까?

해결책

You appear to be mixing server side and client side code. Your app.get function is your server-side handler for requests to /main. You need to send something back via the res object.

Since you have mapped ejs I assume you're server-side views are EJS files. You can render them with something simple like:

app.get('/main', function(req, res) {
  res.render('main'); // this requires a file named main.ejs
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top