Зависимости моего проекта (я полагаю, что некоторые из них не являются, я должен увидеть это позже):
"dependencies": {
"@types/connect-flash": "0.0.35",
"@types/express": "^4.17.2",
"@types/express-flash": "0.0.1",
"@types/express-session": "^1.15.16",
"@types/node-sass-middleware": "0.0.31",
"@types/postcss-modules-values": "^3.0.0",
"body-parser": "^1.19.0",
"bootstrap": "^4.4.1",
"concurrently": "^5.0.2",
"connect-flash": "^0.1.1",
"cookie-parser": "~1.4.4",
"core-js": "^3.6.0",
"cors": "^2.8.5",
"debug": "~2.6.9",
"eslint-config-prettier": "^6.9.0",
"eslint-plugin-prettier": "^3.1.2",
"express": "^4.17.1",
"express-flash": "0.0.2",
"express-session": "^1.17.0",
"http-errors": "~1.6.3",
"jquery": "^3.4.1",
"mongodb": "^3.4.1",
"mongoose": "^5.8.3",
"morgan": "~1.9.1",
"node-sass-middleware": "^0.11.0",
"nodemon": "^2.0.2",
"popper": "^1.0.1",
"popper.js": "^1.16.0",
"postcss-typescript-css": "^0.2.1",
"prettier": "^1.19.1",
"pug": "^2.0.4",
"pug-bootstrap": "0.0.16",
"req-flash": "0.0.3"
},
App.ts (кажется, я где-то видел, что с этой версией express -сессия мне не нужно устанавливать cookieParser, как я делаю)
import flash from "connect-flash";
import cookieParser from "cookie-parser";
import session from "express-session";
private middlewares(): void {
this.express.use(express.json())
this.express.set('views', path.join(__dirname, 'views'));
this.express.set('view engine', 'pug')
this.express.use(express.static(path.join(__dirname, 'public')));
this.express.use(cors());
this.express.use(cookieParser('secret'));
this.express.use(sassMiddleware({
src: __dirname + '/sass',
dest: __dirname + '/public/',
debug: true,
outputStyle: 'compressed',
//prefix: '/stylesheets'
}));
this.express.use(session({
secret: 'restaurantes-app',
resave: false,
saveUninitialized: false,
cookie: { secure: true, maxAge: 60000 },
}))
this.express.use(flash());
RestauranteController.ts:
public async store(req: Request, res: Response, next: NextFunction ){
Restaurante.findOneAndUpdate({
nome: req.body.nome ,
}, req.body, { new: true }, async (err, result) => {
if(err){
res.send(err);
}else
{
if(result == null){
const restaurante = await Restaurante.create(req.body);
restaurante.save((err) => {
if(err){
req.flash('error','Houve um erro ao tentar cadastrar o restaurante: ' + err);
return res.redirect('/restaurantes');
}
else{
req.flash('success', 'your flash messages are working');
return res.redirect('/restaurantes');
}
});
}
else{
return res.redirect('/restaurantes');
}
}
});
};
index.pug
.container-fluid
h1(style="font-size:22px; margin-top: 2%") Restaurantes
.container-fluid
fieldset(class="scheduler-border" style="height:200px; width:600px; border:2px #cccccc solid; border-radius: 10px;")
legend(class="scheduler-border" style="font-size:14px;") Filtros
form(method='get' action='')
div.form-group
label(for='nome') Nome:
input#nome.form-control(type='text', placeholder='nome do restaurante' name='nome')
button.btn.btn-primary(type='submit') Pesquisar
a(href="/restaurantes/create", class="btn btn-primary btn-lg" style='margin-left: 10px;') Cadastrar novo restaurante
div.flashes
if typeof messages.error !== 'undefined' && messages.error.length > 0
p.error #{ messages.error }
if typeof messages.success !== 'undefined' && messages.success.length > 0
p.success #{ messages.success }
На странице ничего не появляется, когда я регистрирую ресторан.