如何在node.js的帮助下使用网站在localhost上工作

How to use a site to work on localhost with the help of node.js?

本文关键字:网站 localhost 工作 帮助 node js      更新时间:2024-05-03

我正在学习node.js,我试图使用谷歌网页在本地主机上工作,它的菜单项将被删除,但它的搜索功能应该在本地主机和网站上一样工作。这是我尝试在localhost上使用谷歌,但在localhost中显示"Can't Get",这是一种错误还是我做错了,请指导我如何实现我想要的工作。我在XP上使用node.js版本5.9.1。

提前谢谢。

search.js

var express = require('express'),   
        app = express();
var bodyParser = require('body-parser');
   compression = require('compression');   
var NLTunnel = require('node-local-tunnel');        
   var options = {
  remoteHost : 'http://www.google.com/',
  localBase : 'http://localhost:3000'
};
   NLTunnel.client(options);    
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(compression());
app.use(express.static('assets/'));
app.listen(3000);
    var express = require('express'),   
       app = express(),
       bodyParser = require('body-parser'),
       compression = require('compression'),
       http = require('http'),
       server = http.createServer(app);
    app.use(bodyParser());   
    app.use('/', express.static('public'));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({extended:true}));
    app.use(compression());
    server.listen(3000);

试试这个。这在我的机器上工作。

我尝试过使用requestcheerio,但我只得到html页面,没有任何cssjs页面(如果有),搜索功能也不起作用。这样做对吗?以及我们如何使搜索功能在本地主机上正常工作。

var httpobj = require('http');
var request = require('request');
var cheerio = require('cheerio');
var all_html;
var url = 'https://www.google.co.in/?gfe_rd=cr&ei=dVHeVuLuG-uK8QeCk6vICw'
request(url, function (error, response, html) {
var $page = cheerio.load(html);
all_html = $page("html");    
}); 

httpobj.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});          
res.write(all_html + ' '); 
res.end('');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');