generated from fionn/ts-template
Initial commit
This commit is contained in:
26
backend/src/app.ts
Normal file
26
backend/src/app.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import express, { NextFunction, Request, Response } from 'express';
|
||||
import router from './router';
|
||||
|
||||
const { PORT = 3000 } = process.env;
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use('/api', router.router);
|
||||
|
||||
const frontendRoot = '/opt/frontend/dist';
|
||||
app.use(express.static(frontendRoot));
|
||||
app.use((req, res) => res.sendFile(`${frontendRoot}/index.html`));
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
app.use((err, req: Request, res: Response, next: NextFunction) => {
|
||||
console.log('err', err);
|
||||
|
||||
// 500
|
||||
res.status(500).json({ msg: 'an error occurred' });
|
||||
});
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(
|
||||
`application is listening on port ${PORT}`,
|
||||
);
|
||||
});
|
||||
14
backend/src/controllers/foo.controller.ts
Normal file
14
backend/src/controllers/foo.controller.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { Request, Response } from 'express';
|
||||
import fooService from '../services/foo.service';
|
||||
|
||||
function getFoo(req: Request, res: Response) {
|
||||
res.json({
|
||||
foo: true,
|
||||
msg: fooService.getFooDetails(),
|
||||
data: req.body,
|
||||
});
|
||||
}
|
||||
|
||||
export default {
|
||||
getFoo,
|
||||
};
|
||||
14
backend/src/router.ts
Normal file
14
backend/src/router.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { Request, Response, Router } from 'express';
|
||||
import fooController from './controllers/foo.controller';
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get('/foo', fooController.getFoo);
|
||||
// aaaaa
|
||||
|
||||
router.use((req: Request, res: Response) => {
|
||||
// 404
|
||||
res.status(404).json({ msg: 'the requested resource could not be found' });
|
||||
});
|
||||
|
||||
export default { router };
|
||||
7
backend/src/services/foo.service.ts
Normal file
7
backend/src/services/foo.service.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
function getFooDetails() {
|
||||
return 'foo';
|
||||
}
|
||||
|
||||
export default {
|
||||
getFooDetails,
|
||||
};
|
||||
Reference in New Issue
Block a user