Search code examples
javascriptnode.jsosc

Node.js HTTP Request to send OSC message


I am new to Node.js and am wondering if anyone has an idea on how I'd go about making a program that sends an OSC message when someone makes an HTTP request to my server?

I have a simple program running right now that when you run it it sends an OSC message, how might I expand it so this message gets sent anytime a certain HTTP request is made?

var osc = require('node-osc');

var client = new osc.Client('127.0.0.1', 3333);
client.send('/1', 1);

Thanks


Solution

  • To receive HTTP requests, you need an HTTP server. Node has a built-in HTTP server. To handle simple routing, I usually go straight for Express which sets up a nice middleware stack and some other helpful bits as you build out your application. From the documentation:

    var express = require('express');
    var app = express();
    
    app.get('/', function(req, res){
      res.send('hello world');
    });
    
    app.listen(3000);
    

    Now, what I would do is set up a specific endpoint for OSC messages:

    app.all('/osc', functin (req, res) {
       oscClient.send('/1', 1);
    }
    

    You can add parameters and what not if you want Express to handle that for you in the URL, or you can get at the query string or POST data directly from the req object.