Skip to content

Commit

Permalink
fix backend, add .vscode in order to keep settings for vscode/project…
Browse files Browse the repository at this point in the history
… in repo
  • Loading branch information
daniel committed Nov 20, 2019
1 parent c3c2d6c commit 6b5e81b
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 38 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
node_modules
.env
.vscode
dist
dist
20 changes: 20 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Node Inspector",
"type": "node",
"request": "launch",
"args": ["${workspaceRoot}/src/index.ts"],
"runtimeArgs": ["-r", "ts-node/register"],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart",
"env": {
"TS_NODE_IGNORE": "false"
}}
]
}
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use Node Inspector to debug in vscode
52 changes: 52 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@types/node": "^12.12.5",
"@types/supertest": "^2.0.8",
"@zerollup/ts-transform-paths": "^1.7.4",
"cross-env": "^6.0.3",
"jest": "^24.9.0",
"nodemon": "^1.19.4",
"npm-run-all": "^4.1.5",
Expand All @@ -31,7 +32,7 @@
},
"scripts": {
"build": "rm -rf ./dist && ttsc",
"dev": "nodecross-env NODE_ENV=development tsnd --inspect --respawn src/main.tmon",
"dev": "cross-env NODE_ENV=development node --inspect --respawn src/index.ts",
"lint": "npm run lint:ts && npm run lint:eslint",
"lint:ts": "tsc --noEmit",
"lint:eslint": "eslint src/**/*.ts",
Expand Down
12 changes: 7 additions & 5 deletions src/middleware/puppies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const readFileSync = util.promisify(fs.readFile);
const storePath = path.join(__dirname, '/../../store/store.json');
const reStorePath = path.join(__dirname, '/../../store/reset.json');

const getStoreData = async (aPath = storePath): Promise<IStore> => {
const getStoreData = async (aPath=storePath): Promise<IStore> => {
const data = await readFileSync(aPath, 'utf-8');
return JSON.parse(data);
};
Expand Down Expand Up @@ -53,7 +53,7 @@ const reStore = async (_req: Request, res: Response, next: NextFunction) => {
message
};
next();
} catch (er) {
} catch(er) {
console.log(er);
}
};
Expand Down Expand Up @@ -81,11 +81,13 @@ const adoptPuppy = async (req: Request, res: Response, next: NextFunction) => {
const { params: { id } } = req;
if (id) {
const { puppies } = await getStoreData();
const puppy = puppies.find((apuppy) => apuppy.id === parseInt(id, 10));
const index = puppies.findIndex((apuppy) => apuppy.id === parseInt(id, 10));
puppies[index].adopted = !puppies[index].adopted;
writeStoreData(puppies);

res.locals = {
...res.locals,
puppy
puppy: puppies[index]
};
next();
}
Expand All @@ -107,7 +109,7 @@ const deletePuppy = async (req: Request, res: Response, next: NextFunction) => {

writeStoreData(puppies);
message = {
body: `${puppy[0].name} - remove with success`,
body: `${puppy[0].id}`,
type: 'success'
};
} else {
Expand Down
37 changes: 21 additions & 16 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,60 @@
import bodyParser from 'body-parser';
import cors from 'cors';
import express from 'express';
import { deletePuppy, getPuppies, getPuppy, reStore, adoptPuppy } from './middleware/puppies';
import * as action from './middleware/puppies';

const app = express();
console.log('enable cors');
// Aapp.use(cors());

app.use(cors({ origin: 'http://localhost:3000' }));

app.use(cors());
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));

// parse application/json
app.use(bodyParser.json());

// app.use((req, res) => {
// console.log(req.body);
// res.on("finish", () => {
// console.log(res);
// });
// });

app.get('/', (_req, res) => {
res.send('Hello Buba3!');
});

app.get('/puppies', getPuppies, (_req, res) => {
app.get('/puppies', action.getPuppies, (_req, res) => {
const { puppies } = res.locals;

res.json(puppies);
});

app.get('/puppy/:id', getPuppy, (_req, res) => {
app.get('/puppy/:id', action.getPuppy, (_req, res) => {
const { puppy } = res.locals;

res.json(puppy);
});

app.patch('/adoptPuppy/:id', adoptPuppy, (_req, res) => {
app.delete('/puppy/:id', action.deletePuppy, (_req, res) => {
const { message } = res.locals;
const status = message.type === 'success' ? 204 : 404;

res.status(status).send(message);
res.send(message.body).status(status);
});

app.delete('/puppy/:id', deletePuppy, (_req, res) => {
app.post('/restore', action.reStore, (_req, res) => {
const { message } = res.locals;
const status = message.type === 'success' ? 204 : 404;

res.status(status).send(message);
});

app.post('/restore', reStore, (_req, res) => {
const { message } = res.locals;
const status = message.type === 'success' ? 204 : 404;
app.patch('/adoptPuppy/:id', action.adoptPuppy, (_req, res) => {
const { puppy } = res.locals;
const status = puppy ? 204 : 404;

res.status(status).send(message);
res.send(puppy).status(status);
});

export default app;


export default app;
16 changes: 2 additions & 14 deletions store/store.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"name": "tikki",
"type": "biki",
"id": 1,
"adopted": false
"adopted": true
},
{
"name": "aka",
"type": "baka",
"id": 2,
"adopted": false
"adopted": true
},
{
"name": "meke",
Expand All @@ -23,18 +23,6 @@
"type": "bala",
"id": 4,
"adopted": true
},
{
"name": "riki",
"type": "piki",
"id": 5,
"adopted": true
},
{
"name": "beny",
"type": "penny",
"id": 6,
"adopted": false
}
]
}

0 comments on commit 6b5e81b

Please sign in to comment.