Friday 2 June 2017

How to start node.js socket.io server from within a brackets extension

I'm trying to write a brackets extension in which data is send to a server. In the main.js file I want to execute a server.js file in which I create a node.js socket.io server. When it is finished, the extension should be able to send data to that server.

My directory looks like this:

|-- myExtensionFolder
    |-- node
        |-- node_modules
        |-- server.js
    |-- main.js
    |-- package.json

My server.js file currently contains this:

var server = require("http").createServer(),
socket = require("socket.io")(server);

socket.on("connect", function(){
    console.log("connected");
});

socket.on("event", function(data){
    console.log("event");
});

socket.on("disconnect", function(){
    console.log("disconnected");
});

server.listen(3000);

And my main.js file contains this:

define(function (require, exports, module) {
    "use strict";

    console.log("start extension");

    // The CommandManager registers command IDs with functions
    var CommandManager = brackets.getModule("command/CommandManager"),
    // This holds the list of all default commands
    Commands = brackets.getModule("command/Commands");

    var serverModule = require("node/server");
    //I'm trying to get the server.js file here and would like to somehow execute it 
})

So my question is: How can I start the server from the server.js file inside the main.js file?

And if some of you are wondering why I don't have an index.html file... I'm not going to need a website, only a server where I can transfer data.



via Kerstin M.

No comments:

Post a Comment