Skip to content

Commit

Permalink
添加 node 转发请求功能
Browse files Browse the repository at this point in the history
  • Loading branch information
superman66 committed Mar 15, 2017
1 parent 9f9c83c commit 01cb80d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
4 changes: 2 additions & 2 deletions config/index.js
Expand Up @@ -22,13 +22,13 @@ module.exports = {
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {
'/api': {
/*'/api': {
target: 'http://api.douban.com/v2',
changeOrigin: true,
pathRewrite: {
'^/api': ''
}
}
}*/
},
// CSS Sourcemaps off by default because relative paths are "buggy"
// with this option, according to the CSS-Loader README
Expand Down
52 changes: 52 additions & 0 deletions node-proxy/index.js
@@ -0,0 +1,52 @@
/**
* Created by superman on 17/3/15.
*/

var express = require('express');
var request = require('superagent')

var app = express();
var HOST = 'http://api.douban.com/v2'

/**
* CORS support.
*/

app.all('*', function (req, res, next) {
if (!req.get('Origin')) return next();
// use "*" here to accept any origin
res.set('Access-Control-Allow-Origin', '*');
res.set('Access-Control-Allow-Methods', 'GET');
res.set('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
// res.set('Access-Control-Allow-Max-Age', 3600);
if ('OPTIONS' == req.method) return res.send(200);
next();
});

app.get('/movie/:type', function (req, res) {
var sreq = request.get(HOST + req.originalUrl)
sreq.pipe(res);
sreq.on('end', function (error, res) {
console.log('end');
});
})

app.get('/movie/subject/:id', function (req, res) {
var sreq = request.get(HOST + req.originalUrl)
sreq.pipe(res);
sreq.on('end', function (error, res) {
console.log('end');
});
})

app.get('/movie/search', function (req, res) {
var sreq = request.get(HOST + req.originalUrl)
sreq.pipe(res);
sreq.on('end', function (error, res) {
console.log('end');
});
})

app.listen(8081, function () {
console.log('HTTP Server is running in http://127.0.0.1:8081')
})
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -16,7 +16,9 @@
"vue": "^2.1.0",
"vue-infinite-scroll": "^2.0.0",
"vue-router": "^2.1.1",
"vuex": "^2.0.0"
"vuex": "^2.0.0",
"express": "^4.15.2",
"superagent": "^3.5.0"
},
"devDependencies": {
"autoprefixer": "^6.4.0",
Expand Down
6 changes: 3 additions & 3 deletions src/store/api.js
Expand Up @@ -4,7 +4,7 @@
import axios from 'axios';

// 使用代理
const HOST = '/api/';
const HOST = 'http://localhost:8081';

export const API_TYPE = {
movie:{
Expand All @@ -29,11 +29,11 @@ export function fetchItemByType(type) {


export function fetchMoviesByType(type, start=0, count=20) {
return fetchItemByType(`movie/${type}?start=${start}&count=${count}`)
return fetchItemByType(`/movie/${type}?start=${start}&count=${count}`)
}

export function fetchMovieById(id) {
return fetch(`movie/subject/${id}`);
return fetch(`/movie/subject/${id}`);
}

export function fetchSearchMovies(query, start=0) {
Expand Down

0 comments on commit 01cb80d

Please sign in to comment.