mirror of
				https://github.com/usatiuk/ustk-todolist.git
				synced 2025-10-28 15:47:48 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			62 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			62 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| require('dotenv').config();
 | |
| 
 | |
| const config = require('./config');
 | |
| const db = require('./config/db');
 | |
| const app = require('./config/app');
 | |
| 
 | |
| require('./models/TodoList');
 | |
| require('./models/Todo');
 | |
| 
 | |
| app.use('/lists', require('./routes/lists'));
 | |
| 
 | |
| // 404 route
 | |
| app.use((req, res) => {
 | |
|   res.status(404);
 | |
| 
 | |
|   if (req.accepts('html')) {
 | |
|     res.send('404');
 | |
|     return;
 | |
|   }
 | |
| 
 | |
|   if (req.accepts('json')) {
 | |
|     res.send({ error: 'Not found' });
 | |
|     return;
 | |
|   }
 | |
| 
 | |
|   res.type('txt').send('not found');
 | |
| });
 | |
| 
 | |
| // handle errors
 | |
| app.use((error, req, res, next) => {
 | |
|   switch (error.name) {
 | |
|     case 'ValidationError':
 | |
|       res.status(400);
 | |
|       res.json({ success: false, error });
 | |
|       break;
 | |
|     case 'NotFound':
 | |
|       res.status(404);
 | |
|       res.json({ success: false, error });
 | |
|       break;
 | |
|     case 'BadRequestError':
 | |
|       res.status(400);
 | |
|       res.json({ success: false, error });
 | |
|       break;
 | |
|     default:
 | |
|       res.status(500);
 | |
|       res.json({ success: false, error });
 | |
|   }
 | |
|   next(error);
 | |
| });
 | |
| 
 | |
| let server;
 | |
| if (process.env.NODE_ENV !== 'test') {
 | |
|   db.connect();
 | |
|   server = app.listen(config.app.port, () => {
 | |
|     console.log('Started!');
 | |
|   });
 | |
| } else {
 | |
|   server = app;
 | |
| }
 | |
| 
 | |
| module.exports = server;
 |