简体   繁体   English

使用next.js和express.js以及使用会话的最佳实践是什么?

[英]What are the best practices to work with next.js and express.js and use sessions

for a few weeks now i've started a project using node.js and express.js , I've also added React objects in the front. 几周以来,我已经使用node.jsexpress.js开始了一个项目,我还在前面添加了React对象。

I developped a full session manager using express-session and connect-mongodb-session and it works great. 我使用express-sessionconnect-mongodb-session开发了一个完整的会话管理器,它运行得很好。

I have one thing left to add now to have everything set up : next.js . 我现在要添加一件事来设置所有内容: next.js And here it gets a little more complicated to me. 在这里,它对我来说有点复杂。

My purpose is to have the same thing I have now but with a smooth transition between pages. 我的目的是拥有与现在相同的东西,但页面间平滑过渡。


I started using the official example from git : https://github.com/zeit/next.js/tree/canary/examples/custom-server-express 我开始使用git中的官方示例: https//github.com/zeit/next.js/tree/canary/examples/custom-server-express

Which is very nice. 哪个非常好。 But the pages in /page are public ? 但/页面中的页面是公开的吗? Someone could access them by entering their adress, which is not very good because sessions should be completly waterproof. 有人可以通过输入他们的地址来访问它们,这不是很好,因为会话应该完全防水。

My first solution is to call them random names so people can't access them directly, but it's not very neat ... And they could still workarround ... This would make me write somehing like this : 我的第一个解决方案是将它们称为随机名称,以便人们无法直接访问它们,但它不是很整洁......而且它们仍然可以工作...这会让我写下这样的东西:

 server.get('/nice_name', (req, res) => { 
    console.log('loaded') return app.render(req, res, '/uoiuouiorlos_ugly_random_name', req.query) })

My other solution is to make a route for their adress and call (req,res,next) => next() 我的另一个解决方案是为他们的地址和呼叫建立一条路线(req,res,next)=> next()

This could work but ... is it pretty ? 这可行,但......它很漂亮吗?

Another solution is to edit next.config.js and write : module.exports = { useFileSystemPublicRoutes: true, } 另一个解决方案是编辑next.config.js并写:module.exports = {useFileSystemPublicRoutes:true,}

it works great but then every page is loading like they would normally do ... So what would be the point ? 它工作得很好但是每个页面都像往常一样加载......那么重点是什么呢?

const express = require('express')
const next = require('next')

const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()

console.log('page loaded')

app.prepare().then(() => {
const server = express()

server.get('/', (req, res) => {
    console.log('loaded')
    return app.render(req, res, '/1A', req.query)
})

server.get('/space2', (req, res) => {
    console.log('loaded')
    return app.render(req, res, '/2A', req.query)
})

server.get('*', (req, res) => {
    return handle(req, res)
})

server.listen(port, err => {
    if (err) throw err
    console.log(`> Ready on http://localhost:${port}`)
})
})

I'm a beginner using next, thank you in advance for your help ! 我是初学者,请提前感谢您的帮助!

for now redirecting to a page named 404.js if people try to access the actual page works fine 现在重定向到名为404.js的页面如果人们试图访问实际页面工作正常

express.get('/', (req, res) => {
    return app.render(req, res, '/landing_page', req.query);
})

express.get('/landing_page', (req, res) => {
        return app.render(req, res, '/404', req.query)
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM