Skip to content

Commit

Permalink
add: server
Browse files Browse the repository at this point in the history
  • Loading branch information
Yangfan2016 committed May 24, 2019
1 parent f705c47 commit e91cb37
Show file tree
Hide file tree
Showing 7 changed files with 866 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ psd
thumb
sketch


/build
/server/build
2 changes: 1 addition & 1 deletion config/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const resolveModule = (resolveFn, filePath) => {
module.exports = {
dotenv: resolveApp('.env'),
appPath: resolveApp('.'),
appBuild: resolveApp('build'),
appBuild: resolveApp('server/build'),
appPublic: resolveApp('public'),
appHtml: resolveApp('public/index.html'),
appIndexJs: resolveModule(resolveApp, 'src/index'),
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
"scripts": {
"start": "node scripts/start.js",
"build": "node scripts/build.js",
"test": "node scripts/test.js"
"test": "node scripts/test.js",
"serve": "node server/controllers/index.js"
},
"eslintConfig": {
"extends": "react-app"
Expand Down
45 changes: 45 additions & 0 deletions server/controllers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const Koa = require("koa");
const Router = require("koa-router");
const proxy = require("koa-server-http-proxy");
const static = require("koa-static");
const fs = require("fs");
const path = require("path");

const router = Router();
const app = new Koa;

const PORT = 9876;


// views
router.get("/", async (ctx, next) => {
let str = fs.readFileSync(path.resolve(__dirname, "../build/index.html"), "utf-8");
ctx.response.body = str;
});

// 404
// router.get("*", async (ctx, next) => {
// ctx.response.redirect("/");
// })

app.use(proxy('/api', {
target: 'http://api.douban.com/',
changeOrigin: true,
pathRewrite: {
'^/api': '/v2', // 重写路径
},
}));
app.use(proxy('/bing', {
target: 'https://www.bing.com/',
changeOrigin: true,
pathRewrite: {
'^/bing': '/', // 重写路径
},
}));
app.use(static(__dirname, "../build"));
app.use(router.routes());

app.listen(PORT, () => {
console.log(`http://localhost:${PORT}`);
});

7 changes: 5 additions & 2 deletions server/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"name": "douban-movie",
"name": "douban-movie-server",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"private": true,
"dependencies": {
"koa": "^2.7.0"
"koa": "^2.7.0",
"koa-router": "^7.4.0",
"koa-server-http-proxy": "^0.1.0",
"koa-static": "^5.0.0"
}
}
Empty file removed server/src/index.js
Empty file.
Loading

0 comments on commit e91cb37

Please sign in to comment.