Fala pessoal. Tudo bem ?
Eu gostaria de utilizar o consign com o express na versão 4x mais.
Pórem ao tentar utilizar conforme ensinado no curso Aplicações Serverless: Construindo uma API REST retorna o seguinte erro:
Obs: também estou utilizando serverless
offline: ANY /prd/produto (λ: app)
consign v0.1.6 Initialized in /home/ximbinha/myspace/serverLessConsingRoutes
+ ./routes/produto.js
offline: Failure: Cannot read property 'apply' of undefined
TypeError: Cannot read property 'apply' of undefined
at /home/ximbinha/myspace/serverLessConsingRoutes/node_modules/express/lib/router/index.js:635:15
at next (/home/ximbinha/myspace/serverLessConsingRoutes/node_modules/express/lib/router/index.js:210:14)
at Function.handle (/home/ximbinha/myspace/serverLessConsingRoutes/node_modules/express/lib/router/index.js:174:3)
at Function.router (/home/ximbinha/myspace/serverLessConsingRoutes/node_modules/express/lib/router/index.js:47:12)
at Consign.into (/home/ximbinha/myspace/serverLessConsingRoutes/node_modules/consign/lib/consign.js:240:17)
at Object.<anonymous> (/home/ximbinha/myspace/serverLessConsingRoutes/app.js:18:6)
at Module._compile (internal/modules/cjs/loader.js:1198:30)
at Module._compile (pkg/prelude/bootstrap.js:1394:32)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1218:10)
at Module.load (internal/modules/cjs/loader.js:1047:32)
at Function.Module._load (internal/modules/cjs/loader.js:935:14)
at Module.require (internal/modules/cjs/loader.js:1087:19)
at Module.require (pkg/prelude/bootstrap.js:1338:31)
at require (internal/modules/cjs/helpers.js:73:18)
at Object.<anonymous> (/home/ximbinha/myspace/serverLessConsingRoutes/handler.js:3:23)
at Module._compile (internal/modules/cjs/loader.js:1198:30)
at Module._compile (pkg/prelude/bootstrap.js:1394:32)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1218:10)
at Module.load (internal/modules/cjs/loader.js:1047:32)
at Function.Module._load (internal/modules/cjs/loader.js:935:14)
at Module.require (internal/modules/cjs/loader.js:1087:19)
at Module.require (pkg/prelude/bootstrap.js:1338:31)
at require (internal/modules/cjs/helpers.js:73:18)
at /home/ximbinha/myspace/serverLessConsingRoutes/node_modules/serverless-offline/dist/lambda/handler-runner/in-process-runner/InProcessRunner.js:157:133
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async InProcessRunner.run (/home/ximbinha/myspace/serverLessConsingRoutes/node_modules/serverless-offline/dist/lambda/handler-runner/in-process-runner/InProcessRunner.js:157:9)
Arquivos:
handler.js
'use strict';
const app = require('./app');
const serverless = require('serverless-http')
module.exports.run = serverless(app);
app.js
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const helmet = require('helmet');
const cors = require('cors');
const consign = require('consign');
const app = express();
app.use(cors());
app.use(helmet());
app.use(express.json({ strict: false, type: function () { return true } }));
app.use(express.urlencoded({extended: true}));
consign()
.include('routes')
.into(app);
module.exports = app;
produto.js (a parte comentado é como foi passada no curso)
'use strict';
const express = require('express')
const router = express.Router()
router.get('/produto', (req, res) => {
res.status(200).send({message: 'listing produto'});
});
module.exports = router
// module.exports = app => {
// app.get('/produto', (req, res) => {
// res.status(200).send({message: 'listing produto'});
// });
// app.post('/produto', (req, res) => {
// res.status(201).send({message: 'creating a produto'});
// });
// app.put('/produto/:id', (req, res) => {
// const id = req.params.id;
// res.status(200).send({message: `update produto ${id}`});
// });
// app.delete('/produto/:id', (req, res) => {
// const id = req.params.id;
// res.status(204).send({message: `delete produto ${id}`});
// });
// };