chore: Setup typescript for the express app

This commit is contained in:
Faruk AYDIN
2021-10-03 18:55:01 +02:00
committed by Ali BARIN
parent 701fc48d6f
commit fbc2b1e00a
10 changed files with 604 additions and 149 deletions

View 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');
});

View File

@@ -1,7 +0,0 @@
'use strict';
module.exports = backend;
function backend() {
// TODO
}

View File

@@ -0,0 +1,7 @@
import { Request, Response } from 'express';
const indexRouter = (_request: Request, response: Response) => {
return response.json({ hello: 'world!' })
};
export default indexRouter;