Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs to provide 1 api endpoint e one html page

Tags:

node.js

here is my issue. I never wrote something in node without using express so i find it difficult to create a server with basic APIs.

Basically what I found on the internet is this:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

however I don't see How I can implement /index.html and /getData. This code will run on a Raspberry pie, this is why I shouldnt use libraries. Basically I have no much space.

Many thanks, h

like image 511
piggyback Avatar asked Sep 14 '25 23:09

piggyback


2 Answers

You'll need to manually inspect the URL in the request and handle each case separate:

var http = require('http');
http.createServer(function (req, res) {

  if(req.url == "/index.html") {
     fs.readFile("index.html", function(err, text){
       res.setHeader("Content-Type", "text/html");
       res.end(text);
     });
     return;
  }

  if(req.url == "/getData") {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('get data\n');
    return;
  }

  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');

}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
like image 171
Hector Correa Avatar answered Sep 17 '25 14:09

Hector Correa


Just because the code is running on a Pi doesn't mean you can't use modules. Modules don't necessarily take up inordinate space. You'd still have to write the same code many modules have written for you. Modules like Express exist specifically to address the implementation issues you're facing, so you would have to rewrite a router to handle the request for /getData or a file reader for static assets like HTML. Whether you use a module or not, you're going to end up with approximately the same amount of code.

Rather than recreating the wheel, perhaps you just need slimmer modules stripped of the features you don't need. Connect, which Express is based on, is fairly minimal. With either Express or Connect, you can strip out what you don't need. Remember that modules are just a node_modules subdirectory in your project root, so you can remove extraneous things like the tests, examples, and (in several cases) the features you don't need.

Alternatively, there are many slimmer modules like send, which is purely for serving static content like your index page. Journey, which provides JSON-only, is a lean feature set too. The point is there are tons of modules that serve specific needs if Express is too large. The node modules list is a good starting point for finding such modules. Working with node without using modules is kind of like taking a step back to the early days of node when these issues plagued everyone. Of course you can do that, but it has already been done.

like image 26
Corey Avatar answered Sep 17 '25 12:09

Corey