chore: Setup typescript for the express app
This commit is contained in:
34
packages/backend/src/app.ts
Normal file
34
packages/backend/src/app.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import createError from 'http-errors';
|
||||
import express, { Request, Response, NextFunction } from 'express';
|
||||
import logger from 'morgan';
|
||||
|
||||
import indexRouter from './routes/index';
|
||||
|
||||
const app = express();
|
||||
const port = process.env.PORT || '3000';
|
||||
|
||||
app.use(logger('dev'));
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({ extended: false }));
|
||||
|
||||
app.use('/', indexRouter);
|
||||
|
||||
app.listen(port, () => {
|
||||
return console.log(`Server is listening on ${port}`)
|
||||
})
|
||||
|
||||
// catch 404 and forward to error handler
|
||||
app.use(function(req: Request, res: Response, next: NextFunction) {
|
||||
next(createError(404));
|
||||
});
|
||||
|
||||
// error handler
|
||||
app.use(function(err: any, req: Request, res: Response, _next: NextFunction) {
|
||||
// set locals, only providing error in development
|
||||
res.locals.message = err.message;
|
||||
res.locals.error = req.app.get('env') === 'development' ? err : {};
|
||||
|
||||
// render the error page
|
||||
res.status(err.status || 500);
|
||||
res.render('error');
|
||||
});
|
@@ -1,7 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = backend;
|
||||
|
||||
function backend() {
|
||||
// TODO
|
||||
}
|
7
packages/backend/src/routes/index.ts
Normal file
7
packages/backend/src/routes/index.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { Request, Response } from 'express';
|
||||
|
||||
const indexRouter = (_request: Request, response: Response) => {
|
||||
return response.json({ hello: 'world!' })
|
||||
};
|
||||
|
||||
export default indexRouter;
|
Reference in New Issue
Block a user