Skip to content
Snippets Groups Projects
Commit eae6271b authored by Oshani Weerakoon's avatar Oshani Weerakoon
Browse files

example 3

parent 92a5ac28
No related branches found
No related tags found
No related merge requests found
Choosing HTTP/2 and JSON for a Node.js Server
For this example, let's create a simple Node.js server that uses HTTP/2 and JSON for efficient data transfer.
Step 1: Setting up the project
Initialize a new Node.js project:
bash
Copy code
npm init -y
Install the necessary dependencies (assuming you have Node.js 8.4.0 or higher, which includes HTTP/2 support):
bash
Copy code
npm install express
Step 2: Create a simple HTTP/2 server
To use HTTP/2 with Node.js, you'll need to create an SSL certificate for local development:
bash
Copy code
openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' -keyout localhost-privkey.pem -out localhost-cert.pem
Now, let's set up the server:
javascript
Copy code
const express = require('express');
const fs = require('fs');
const http2 = require('http2');
const app = express();
app.get('/data', (req, res) => {
const data = { message: "Hello, this is a lightweight JSON message!" };
res.json(data); // Send JSON response
});
const options = {
key: fs.readFileSync('localhost-privkey.pem'),
cert: fs.readFileSync('localhost-cert.pem')
};
// Create HTTP/2 server
const server = http2.createSecureServer(options, app);
const PORT = 3000;
server.listen(PORT, () => {
console.log(`HTTP/2 server running on https://localhost:${PORT}`);
});
Step 3: Running the server and testing
Run the server with:
bash
Copy code
node yourServerFileName.js
You can test the server with HTTP/2-supporting tools like curl:
bash
Copy code
curl --http2 -k https://localhost:3000/data
The -k flag is used to avoid SSL certificate validation errors since we're using a self-signed certificate for local testing. The --http2 flag forces curl to use HTTP/2.
Conclusion
By using HTTP/2 and a compact message format like JSON, we can ensure that the amount of data transmitted over the network is minimized, which contributes to energy savings. It's important to balance the need for efficiency with the requirements of the application and the capabilities of the client and server when choosing a protocol and message format.
\ No newline at end of file
-----BEGIN CERTIFICATE-----
MIIDCTCCAfGgAwIBAgIUHegPOn/Xy7KXD/qzNtuxnX2mlSMwDQYJKoZIhvcNAQEL
BQAwFDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTIzMTEwNzA5NTAwN1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wkrCMlHsKIRcAcJju1uzzqjArTPBM09J0em7H7JD2lPZeEmX3y7/nmwEkzd
3DtKwTMIK7OSOWM2z8bE7Gmk2kffB6ZvRYRkQ5VAv5wjJmUYv83eknNJAoGAQnGG
/a8CLP12i3K1w/0a5Xi8Nk2M0HBwSZgx/8W7T6wRIWW8o8HCH36XCUeg7Qs1h2bm
fvN9BWS4//o9+Qobj+Z9Y7I0jdDmYEkZZ/j9OU5hNRG/RaGnqxzu/HJDZUz8OKaU
wtsOvyOLsnKmbi1YxyaaGk7Qgxb2sFuwCubjuIECgYArU0VLE0mFU+h1/iRN0tGr
KZxZr4E9w1IUYuTqMjGOmKe5WlHjJJndyMLktPSdJplnX5exVnolKkhH5ywMJNhH
vp5ngmJMjVZGH7hc7qwqjgPVcPcm3R64+qVjGpWa6vbrdDfUUk2lToYDzzQcvymW
Qk88lHotdWqlr2uM1YH8eA==
-----END PRIVATE KEY-----
const express = require('express');
const fs = require('fs');
const http2 = require('http2');
const app = express();
app.get('/data', (req, res) => {
const data = { message: "Hello, this is a lightweight JSON message!" };
res.json(data); // Send JSON response
});
const options = {
key: fs.readFileSync('localhost-privkey.pem'),
cert: fs.readFileSync('localhost-cert.pem')
};
// Create HTTP/2 server
const server = http2.createSecureServer(options, app);
const PORT = 3000;
server.listen(PORT, () => {
console.log(`HTTP/2 server running on https://localhost:${PORT}`);
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment