45 lines
629 B
JavaScript
45 lines
629 B
JavaScript
// src/infra/http/app.js
|
|
|
|
const express = require('express');
|
|
const routes = require('./routes');
|
|
|
|
function createApp() {
|
|
const app = express();
|
|
|
|
app.use(express.json());
|
|
app.use(express.text({ type: '*/*' }));
|
|
|
|
|
|
app.use((req, res, next) => {
|
|
let data = '';
|
|
|
|
req.on('data', chunk => {
|
|
data += chunk;
|
|
});
|
|
|
|
req.on('end', () => {
|
|
if (data && !req.body) {
|
|
req.rawBody = data;
|
|
|
|
// tenta JSON
|
|
try {
|
|
req.body = JSON.parse(data);
|
|
} catch {
|
|
req.body = data;
|
|
}
|
|
}
|
|
|
|
next();
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
app.use('/api', routes);
|
|
|
|
return app;
|
|
}
|
|
|
|
module.exports = createApp;
|