Skip to Content
🎉 How to create page like this?. Check it out →
DevWebcertificates https

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 localhost trong thÆ° mục gốc của dá»± án để tạo 2 file localhost.pem và localhost-key.pem
  • Tạo má»™t tệp server.js trong thÆ° mục gốc của dá»± án nhÆ° sau:
    server.js
    const { 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.js hoặc mở package.json và thêm lệnh node server.js
    package.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