This is a simple library to generate a self-signed x509 key-pair and certificate.
Import library from the ESM.SH CDN for fast and easy setup:
import{create}from"//esm.sh/gh/rahatool/self-cert";Use the package manager npm to install self-cert.
$ npm install github:rahatool/self-cert- Create a new project with the entry point "index.mjs".
- Add the project dependency by
npm i github:rahatool/self-cert. - Put the following script in file "index.mjs".
import{createascreateCertificate}from'@raha.group/self-cert';importfsfrom'fs/promises';importosfrom'os';lethosts=[];for(letnetworkInterfaceofObject.values(os.networkInterfaces())){for(letassignedNetworkAddressofnetworkInterface){hosts.push(assignedNetworkAddress.address);}}letcertificate=createCertificate({domains: [// List the hostnames (including wildcards) on your origin that the certificate should protect.'localhost','*.local',// wildcards
...hosts],expires: newDate(2025,1),/* attributes: { commonName: domains[0], countryName: 'IR', stateName: 'Isfahan', locality: 'Isfahan', organizationName: 'None', }, keySize: 2048, */});awaitfs.writeFile('privateKey.pem',certificate.privateKey);awaitfs.writeFile('publicKey.pem',certificate.publicKey);awaitfs.writeFile('certificate.pem',certificate.certificate);Here is an example of generating an SSL key/cert on the fly and running an HTTPS server on port 443. Use https://localhost:443 to access the created server.
import{createascreateCertificate}from'@raha.group/self-cert';importhttpsfrom'https';letcertificate=createCertificate({domains: ['localhost'],});https.createServer({key: certificate.privateKey,cert: certificate.certificate},function(request,response){response.end('Hello World ~_^');}).listen(443);