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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.