title | description | services | author | ms.service | ms.topic | ms.date | ms.author | ms.custom |
---|---|---|---|---|---|---|---|---|
include file |
include file |
service-bus-relay |
clemensv |
service-bus-relay |
include |
05/02/2018 |
clemensv |
include file |
Create a new JavaScript file called listener.js
.
Run npm install hyco-https
from a Node command prompt in your project folder.
-
Add the following constant to the top of the
listener.js
file.const https = require('hyco-https');
-
Add the following constants to the
listener.js
file for the hybrid connection details. Replace the placeholders in brackets with the values you obtained when you created the hybrid connection.const ns
- The Relay namespace. Be sure to use the fully qualified namespace name; for example,{namespace}.servicebus.windows.net
.const path
- The name of the hybrid connection.const keyrule
- The name of the SAS key.const key
- The SAS key value.
-
Add the following code to the
listener.js
file. :You will notice that the code is not much different from any simple HTTP server example you can find in Node.js beginner tutorials, which the exception of using the
createRelayedServer
instead of the typicalcreateServer
function.var uri = https.createRelayListenUri(ns, path); var server = https.createRelayedServer( { server : uri, token : () => https.createRelayToken(uri, keyrule, key) }, (req, res) => { console.log('request accepted: ' + req.method + ' on ' + req.url); res.setHeader('Content-Type', 'text/html'); res.end('<html><head><title>Hey!</title></head><body>Relayed Node.js Server!</body></html>'); }); server.listen( (err) => { if (err) { return console.log('something bad happened', err) } console.log(`server is listening on ${port}`) }); server.on('error', (err) => { console.log('error: ' + err); });
Here is what your listener.js file should look like:
const https = require('hyco-https'); const ns = "{RelayNamespace}"; const path = "{HybridConnectionName}"; const keyrule = "{SASKeyName}"; const key = "{SASKeyValue}"; var uri = https.createRelayListenUri(ns, path); var server = https.createRelayedServer( { server : uri, token : () => https.createRelayToken(uri, keyrule, key) }, (req, res) => { console.log('request accepted: ' + req.method + ' on ' + req.url); res.setHeader('Content-Type', 'text/html'); res.end('<html><head><title>Hey!</title></head><body>Relayed Node.js Server!</body></html>'); }); server.listen( (err) => { if (err) { return console.log('something bad happened', err) } console.log(`server is listening on ${port}`) }); server.on('error', (err) => { console.log('error: ' + err); });