我的index.js中有一个router.post,我试图从用户那里得到一个输入,然后将用户引导到另一个名为question.ejs的页面。 Question.ejs有一个名为question.js的js,用于从网上查看用户的相关文章并以html格式显示它们。问题是我的question.ejs在我的请求完成之前加载,我没有要显示的内容。另一个奇怪的事情,我必须按两次才能使一切正常工作......这一切都很难解释,但我制作了一个关于我的问题的视频,我希望你能帮助我。
我已经制作了几个控制台日志,我已经列出了他们所说的内容,空白旁边的数字表示终端中控制台日志的顺序。
这是我的index.js代码:
var express = require('express');
var router = express.Router();
var news = require('../news.json');
var newsQuestion = require('../newsQuestion.json');
var bodyParser = require('body-parser');
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', {
title: 'Express',
news: news
});
});
var parse = require('./parse');
router.use(bodyParser.urlencoded({ extended: true }));
router.post('/questionUser', function(req,res){
newsQuestion.question.txt = req.body.id;
//var question = require('./question');
question = require('./question');
router.use('/question',question);
console.log("this is after router.use: ", newsQuestion.bbc.title); =>
3**undefined**
res.redirect('question');
});
module.exports = router;
这是我的问题代码.js
var express = require('express');
var router = express.Router();
var news = require('../news.json');
var newsQuestion = require('../newsQuestion.json');
var bodyParser = require('body-parser');
var request = require('request');
var cheerio = require('cheerio');
var linkName = 'http://www.bbc.co.uk/search?
q='+newsQuestion.question.txt+'&sa_f=search-product&filter=news&suggid=';
console.log('this is before request: ', newsQuestion.bbc.title); =>
1**undefined**
request(linkName, function (error, response, html) {
console.log('This is inside of request but before the actual
scrapping'); => 4**undefined**
var titleArray = [];
var linkArray = [];
var img = [];
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
$('a', 'h1').each(function () {
var title = $(this).text().trim();
var link = $(this).attr('href');
if (title.length != 0) {
titleArray.push(title);
linkArray.push(link);
}
});
$('img').each(function () { //#siteTable is a the context and a.title is within the context.
img.push($(this).attr('src'));
});
console.log('This is inside request: ', newsQuestion.bbc.title); => 5*undefined*
newsQuestion.bbc.imgUrl = img[0];
newsQuestion.bbc.title = titleArray[0];
newsQuestion.bbc.url = linkArray[0];
console.log(newsQuestion.bbc.title); => 6*Chile moves towards legalizing abortion in limited cases*
}
});
console.log('this is after request: ',newsQuestion.bbc.title); => 2**undefined**
router.get('/', function(req, res, next) {
res.render('question');
});
这是我的终端:
"C:\Program Files\JetBrains\WebStorm 2017.1.4\bin\runnerw.exe" "C:\Program
Files\nodejs\node.exe" "C:\Users\Archie
Jugdersuren\WebstormProjects\summaproject_trial3 - Copy (3)\bin\www"
this is inside parse
GET / 304 9.783 ms - -
GET /stylesheets/style.css 304 1.505 ms - -
this is before request: undefined
this is after request: undefined
this is after router.use: undefined
POST /questionUser 302 14.087 ms - 60
GET /question 304 1.673 ms - -
GET /stylesheets/style.css 304 0.417 ms - -
This is inside of request but before the actual scrapping
This is inside request: undefined
Chile moves towards legalising abortion in limited cases
以下是我描述我的问题(HD)的视频:
https://www.youtube.com/watch?v=y1VSu1DylFQ
谢谢。
答案 0 :(得分:0)
是的我想通了..它是一种盗版,我只做了两个独立的功能..一个持有请求而另一个持有res.render,我确保res.render等待请求被调用时为3秒。我认为有足够的时间来完成请求....如果有更好的方法请告诉我。