dir.by  
  Поиск  
Компьютер, программы
Node.js (this is a web server that executes JS files)
 Create https localhost certificate for nodejs 
посмотрели 6626 раз
обновлено: 10 March 2021
Step 1. Open Visual Studio Code
Если у вас не установлена Visual Studio Code нужно установить Visual Studio Code ...

Открываем Visual Studio Code
Step 2. Create a new folder my_https_code on disk D: and in the Visual Studio Code select this folder
Создаем новую папку my_https_code на диске D: и
в Visual Studio Code нажимаем
FileOpen Folder ...
и выбираем эту папку D:/my_https_code
Step 3. Create a new file server.js inside Visual Studio Code
Добавим код в
  Файл server.js
const express = require('express')
const app = express()
const https = require('https')
const fs = require('fs')
const port = 3000

app.get('/', (req, res) => {
     res.send("IT'S WORKING!")
})

const httpsOptions = {
     key: fs.readFileSync('./security/cert.key'),
     cert: fs.readFileSync('./security/cert.crt')
}
const server = https.createServer(httpsOptions, app)
     .listen(port, () => {
          console.log('server running at ' + port)
     })
Step 4. In the terminal string, we execute
Let's open the terminal line:
Click TerminalNew Terminal

Введем
npm install express
npm install https
npm install fs
получится вот так:
Step 5. Для создания сертификата в терминальной строке выполним команды
  Command Prompt (Win Console)  
npm install -g mkcert
mkcert create-ca --organization "MyOrg" --validity 3650
mkcert create-cert --ca-key "ca.key" --ca-cert "ca.crt" --validity 3650
MyOrg это название вашей организации и может быть любым.
У меня ошибка!

mkcert create-ca is not digitally signed. You cannot run this script on the current system.
Step 6. Сделаем доступ
Выполним
Set-ExecutionPolicy unrestricted
Step 7. В Visual Studio Code еще раз выполним команды
mkcert create-ca --organization "MyOrg" --validity 3650
mkcert create-cert --ca-key "ca.key" --ca-cert "ca.crt" --validity 3650
И увидим что нет ошибок и сертификат создался
В результате в папке D:\my_https_code\ создались файлы:
ca.crt
ca.key
cert.crt
cert.key

переложим файлы в папку D:\my_https_code\security
ca.crt
ca.key
cert.crt
cert.key
Step 8. Зарегистрируем сертификат в системе
В Explorer нажмем левой клавишей мыши 2 раза на файле cert.crt
Перезагрузим компьютер. Чтобы перезагрузилсь сертификаты
Step 8. Все готово. Проверяем
Откроем браузер Google Chrome.
Откроем ссылку https://localhost:3000
Увидим что https работает:
 
← Previous topic
Chat (user writes a message and others get a message) using socket.io in Node.js (project in Visual Studio)
 
Next topic →
What are modules in Node.js ?
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

  Объявления  
  Объявления  
 
What is Node.js ?
How do I find out the version of Node.js?
Installing Node.js (download and install for Windows)
How do I update my Node.js version (install the latest version)?
npm in Node.js
What is npm in Node.js?
How do I find the npm (Node.js) version?
How do I update my npm version (install the latest version) ? | Node.js
File package.json in Node.js
Difference Between Tilde(~) and Lid(^) in package.json | Node.js
File package-lock.json in Node.js
"npm init" | Result: Creates an empty package.json file and populates that file with the default data | Node.js
"npm install package_name" | Result: Installs JavaScript Library (Package) | For example, run "npm install jquery" | will add the jQuery library to the node_modules folder | Node.js
"npm install" | Result: Installs the JavaScript libraries (packages) that are specified in the package.json | Node.js
"npm list -g" (see a list of all installed global packages) | Node.js
"npm install -g package_name" (global package install) | Node.js
"npm uninstall -g package_name" (global package removal) | Node.js
Run "npm run EvgenConvertCSS" | The package.json file like this: "scripts": {"EvgenConvertCSS": "node-sass --include-path scss 1.scss 1.css"} | Node.js
Run "npm run EvgenMyCommand" | The file package.json like this: "scripts": {"EvgenMyCommand": "mkdir AAA"} | Node.js
Run "npm run MyScript1" | The file package.json like this: "scripts": {"MyScript1": "npm run MyScript2"} | Node.js
watch option in npm scripts (watch for changes in files)
Debugging NodeJS
Debugging Node.js. Looking at variable values, function stack, breakpoints in Visual Studio Code
Writing Node.js application in a text editor (Notepad, Far)
New Node.js app (create the app in a text editor, run in the console)
Add the express package to the Node.js (in the Windows console)
Uploading an html file and displaying it on the screen in Node.js (creating an application in a text editor, launching it in the console)
Using the socket.io module
A simple application with socket.io in Node.js (creating an application in a text editor, running it in the console)
Writing Node.js project in Visual Studio Code
New Node.js project (create the project in Visual Studio Code)
Create a new Node.js project with websocket (create the project in Visual Studio Code) | client & server
Writing Node.js project in Visual Studio
New Node.js project (create the project in Visual Studio)
Add the express package to Node.js (in Visual Studio)
Uploading an html file and displaying it on the screen in Node.js (project in Visual Studio)
Chat (user writes a message and others get a message) using socket.io in Node.js (project in Visual Studio)
https localhost certificate
Create https localhost certificate for nodejs
How modules are arranged in Node.js (require, exports)
What are modules in Node.js ?
How the require function works inside and what happens when we write require("ModuleName") in Node.js
Creating your own module in Node.js
Writing and connecting your module to Node.js
Built-in Node.js modules
"Express" module Node.js !!!!!!!!!!!!
Hosting Node.js on your website
Hosting and installing Node.js on your website. Configuring Node.js in cPanel
In Node.js I change the js file, and the old cached js file is displayed. Restart Node.js on your website in cPanel
Add npm packages to the Node.js on your site (using cPanel)
Error during WebSocket handshake... Hosting Node.js on your website
Heroku.com free service. Running your Node.js app on Heroku.com
To use Heroku.com you need to install: Git, Node.js
Registration on the Heroku.com website
Installing the command line "Heroku CLI"
"Heroku CLI" is very slow for Windows
Running your Node.js app on Heroku.com
WWW sites for learning Node.js
Sites to learn Node.js

  Ваши вопросы присылайте по почте: info@dir.by