Skip to content

Commit

Permalink
JSAB2-52 Troops backend (green-fox-academy#4)
Browse files Browse the repository at this point in the history
* JSAB2-52, create troops backend

* JSAB2-52, modify files with Router applied

* JSAB2-52, modify package.json since main index.js was moved

* JSAB2-52, fix eslint errors, rename the main file

* JSAB2-52, modify package.json since main index.js was renamed as App.js
  • Loading branch information
hongtaoTang authored and evasimonyi committed Dec 5, 2019
1 parent 8629334 commit dfe0d23
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 19 deletions.
20 changes: 20 additions & 0 deletions backend/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const express = require('express');

const app = express();
const PORT = 4000;
const { troops } = require('./routers');

app.use(express.json());

app.get('/', (req, res) => {
res.status(200).send('Hello world');
});

app.use('/kingdom', troops);

app.use((err, req, res, next) => {
res.status(500).send('Something broke!');
next(err);
});

app.listen(PORT);
6 changes: 3 additions & 3 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"main": "App.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint .",
"start": "node ./routers/index.js",
"start:dev": "nodemon ./routers/index.js"
"start": "node ./App.js",
"start:dev": "nodemon ./App.js"
},
"author": "",
"license": "ISC",
Expand Down
20 changes: 4 additions & 16 deletions backend/routers/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
const express = require('express');
const troops = require('./troops');

const app = express();
const PORT = 4000;

app.use(express.json());

app.get('/', (req, res) => {
res.status(200).send('Hello world');
});

app.use((err, req, res, next) => {
res.status(500).send('Something broke!');
next(err);
});

app.listen(PORT);
module.exports = {
troops,
};
33 changes: 33 additions & 0 deletions backend/routers/troops.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { Router } = require('express');

const router = Router();

const myTroops = {
troops: [
{
id: 1,
level: 1,
hp: 1,
attack: 1,
defence: 1,
started_at: 12345789,
finished_at: 12399999,
}, {
id: 2,
level: 1,
hp: 1,
attack: 1,
defence: 1,
started_at: 12345789,
finished_at: 12399999,
},
],
};

function getTroops(req, res) {
return res.status(200).send(myTroops);
}

router.get('/troops', getTroops);

module.exports = router;

0 comments on commit dfe0d23

Please sign in to comment.