certificates https
SỠdụng SSL trong nextjs
SỠdụng mkcert
- Cà i đặt mkcert .
- Mở terminal chạy lệnh
mkcertđể thamm khảo các bước. - Chạy lệnh
mkcert localhosttrong thư mục gốc cá»§a dá»± án để tạo 2 filelocalhost.pemvÃlocalhost-key.pem - Tạo má»™t tệp
server.jstrong thư mục gốc của dự án như sau:server.jsconst { createServer } = require("https"); const { parse } = require("url"); const next = require("next"); const fs = require("fs"); const port = 3000; const dev = process.env.NODE_ENV !== "production"; const app = next({ dev }); const handle = app.getRequestHandler(); const httpsOptions = { key: fs.readFileSync("./localhost-key.pem"), cert: fs.readFileSync("./localhost.pem") }; app.prepare().then(() => { createServer(httpsOptions, (req, res) => { const parsedUrl = parse(req.url, true); handle(req, res, parsedUrl); }).listen(port, (err) => { if (err) throw err; console.log("ready - started server on url: https://localhost:" + port); }); }); - Cuối cùng chạy lệnh
node server.jshoặc mở package.json và thêm lệnhnode server.jspackage.json... "scripts": { "dev": "node server.js && next dev", "build": "next build", "start": "next start", "lint": "next lint", "prisma:migrate": "npx prisma migrate dev --schema=./src/prisma/schema.prisma" }, ...
Last updated on