Build your own TCP log server with node.js part 1

Eran Goldman-Malka · May 23, 2017

We need to log TCP calls from the ship and give the user option to comment on the fly.

The main problem is that we don’t have time or resources to put in the server,

I decided to implement a quick and dirty log server with rest API, express, socket.io.

Install rest server from scratch :

node.js :

sudo apt-get install curl
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo npm i express -g
sudo npm install -g yo
sudo npm install -g generator-rest</pre>

mongoDB:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
sudo apt-get install -y mongodb</pre>

rest server :

mkdir logserver
cd logserver
yo rest

Next -> Next -> Next …

yo rest:api

? What's the API name? log
? What's the endpoint name? logs
? Where to put the code? src/api
? Which methods it will have? (Press &lt;space&gt; to select, &lt;a&gt; to toggle all, &lt;i&gt; to inverse selection)C
reate (POST), Retrieve list (GET), Retrieve one (GET), Update (PUT), Delete (DELETE)
? Which methods are protected by the master key? (Press &lt;space&gt; to select, &lt;a&gt; to toggle all, &lt;i&gt; to 
inverse selection)
? Which methods are only accessible by authenticated admin users? (Press &lt;space&gt; to select, &lt;a&gt; to to
ggle all, &lt;i&gt; to inverse selection)
? Which methods are only accessible by authenticated users? (Press &lt;space&gt; to select, &lt;a&gt; to toggle a
ll, &lt;i&gt; to inverse selection)
? Do you want to generate a model? Yes
? Which fields the model will have? (comma separated) source,type,message,comment
 create src/api/log/controller.js
 create src/api/log/index.js
 create src/api/log/index.test.js
 create src/api/log/model.js
 create src/api/log/model.test.js
 conflict src/api/index.js
? Overwrite src/api/index.js? overwrite
 force src/api/index.js

run the server for testing : npm start

if you see: Express server listening on http://0.0.0.0:9000, in development mode

then we finish the first part 🙂

now we have a rest server to save the data to the local MongoDB server

Twitter, Facebook