In my app.js, I definite a search router
app.use('/search', require('./router/search'))
In the search.js files, what I do is request a website and response website data, but the router always on dealing.
const express = require('express')
const router = express()
const url = require('url')
const http = require('http')
router.get('/', searchHandler)
function searchHandler(req, res) {
request('http://baidu.com', 'get')
.then(result => {
res.end(result)
})
}
function request(link, method, data) {
return new Promise((resolve, reject) => {
link = url.parse(link)
const result = '';
const options = {
hostname: link.hostname,
port: 80,
path: link.path,
method: method
}
http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
result += chunk;
});
res.on('end', function() {
resolve(result)
})
req.on('error', function(err) {
reject(err);
});
})
})
}
module.exports = router
why does my res.end(result)
not works?