-
Notifications
You must be signed in to change notification settings - Fork 393
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e4b5dd0
commit bf7ce0e
Showing
10 changed files
with
113 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import * as express from 'express'; | ||
import { CounterRoutes } from './routes/CounterRoutes'; | ||
import { DemoRoutes } from './routes/DemoRoutes'; | ||
|
||
const app = express(); | ||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: true })); | ||
|
||
CounterRoutes(app); | ||
DemoRoutes(app); | ||
|
||
// Export the server middleware | ||
module.exports = { | ||
path: '/', | ||
handler: app, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import * as express from 'express'; | ||
|
||
export const CounterRoutes = (app: express.Application) => { | ||
app.put('/counter/increment', (req: express.Request, res: express.Response) => { | ||
setTimeout(() => { | ||
res.status(200).json({ count: parseInt(req.body.count, 10) + 1 }); | ||
}, 200); | ||
}); | ||
|
||
app.put('/counter/decrement', (req: express.Request, res: express.Response) => { | ||
setTimeout(() => { | ||
res.status(200).json({ count: parseInt(req.body.count, 10) - 1 }); | ||
}, 200); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import * as express from 'express'; | ||
// import { serve } from '../utils/Utils'; | ||
// import { getIntInRange } from '@vuesion/utils/dist/randomGenerator'; | ||
|
||
const getIntInRange = (min: number, max: number): number => { | ||
min = Math.ceil(min); | ||
max = Math.floor(max); | ||
return Math.floor(Math.random() * (max - min + 1)) + min; | ||
}; | ||
|
||
const getErrorWithProbability = (probability: number) => getIntInRange(0, 100) <= probability; | ||
|
||
export const DemoRoutes = (app: express.Application) => { | ||
/** | ||
* http -> https redirect for heroku | ||
*/ | ||
app.get('*', (req: express.Request, res: express.Response, next: any) => { | ||
const host: string = req.headers.host || 'localhost:3000'; | ||
const redirect: string = `https://${host}` + req.url; | ||
|
||
if (req.headers['x-forwarded-proto'] && req.headers['x-forwarded-proto'] !== 'https') { | ||
res.redirect(redirect); | ||
} else { | ||
next(); | ||
} | ||
}); | ||
// app.use('/storybook', serve('../../storybook-static')); | ||
app.use('/docs', (req: express.Request, res: express.Response) => { | ||
res.status(301).redirect('https://vuesion.github.io/docs/en/'); | ||
}); | ||
|
||
/** | ||
* Auth-Demo | ||
*/ | ||
app.post('/token', (req: express.Request, res: express.Response) => { | ||
if (getErrorWithProbability(10)) { | ||
res.status(500).json({}); | ||
} else if (req.body.grant_type === 'password') { | ||
res.status(200).json({ access_token: 'accessToken', refresh_token: 'refreshToken' }); | ||
} else if (req.body.grant_type === 'refresh_token' && req.body.refresh_token === 'refreshToken') { | ||
res.status(200).json({ access_token: 'accessToken2', refresh_token: 'refreshToken2' }); | ||
} else if (req.body.grant_type === 'refresh_token' && req.body.refresh_token === 'refreshToken2') { | ||
res.status(200).json({ access_token: 'accessToken', refresh_token: 'refreshToken' }); | ||
} | ||
}); | ||
|
||
app.delete('/token', (req: express.Request, res: express.Response) => { | ||
if (getErrorWithProbability(10)) { | ||
res.status(500).json({}); | ||
} else { | ||
res.status(200).json({}); | ||
} | ||
}); | ||
|
||
app.get('/protected', (req: express.Request, res: express.Response) => { | ||
if (getErrorWithProbability(40)) { | ||
res.status(401).json({}); | ||
} else { | ||
res.status(200).json({}); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Middleware } from '@nuxt/types'; | ||
|
||
const authMiddleware: Middleware = ({ app, store, redirect }) => { | ||
if (!store.getters['auth/isAuthenticated']) { | ||
const redirectArg = '?redirect=' + encodeURI(app.router.history.current.path); | ||
return redirect(app.localePath('/') + redirectArg); | ||
} | ||
}; | ||
|
||
export default authMiddleware; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters