2

Problem :

I am fairly new to node js and find it extremely difficult to separate the frontend from the backend. (I don't fully understand the concepts).

Why ?

I need to convert my web app to a Hybrid app using Apache Cordova.

Specific Case Code :

index.html :

<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: 0.5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
    </style>
  </head>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>


    <script src="/socket.io/socket.io.js"></script>
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
    <script>

      window.prompt('Name ?');
      $('#messages').append($('<li>').text('You joined !'));

  $(function () {
    var socket = io();
    $('form').submit(function(e){
      e.preventDefault(); // prevents page reloading
      socket.emit('chat message', $('#m').val());
      $('#m').val('');
      return false;
    });
    socket.on('chat message', function(msg){
      $('#messages').append($('<li>').text(msg));
    });
  });
    </script>
  </body>
</html>

index.js :

var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', (socket) => {
  console.log('a user connected');
  socket.on('chat message', (msg) => {
      io.emit('chat message', msg);
  });
  socket.on('disconnect', () => {
    console.log('user disconnected');
  });
  socket.on('chat message', (msg) => {
      console.log('message: ' + msg);
  });
});

http.listen(3000, () => {
  console.log('listening on *:3000');
});

I am including package.json dependencies so that you can also, try to fix it :

  "dependencies": {
    "express": "^4.17.1",
    "socket.io": "^2.3.0"
  },

What I tried :

• Searched all GitHub and StackOverflow forums.
• Tried to seperate code myself.
• That's it.

8
  • 2
    What parts are you trying to separate exactly? In that specific code example, the html is frontend for Cordova. The index.js and deps are the backend node service. Commented Jul 15, 2020 at 5:17
  • Yes but how can I serve to the index.html when I convert it to an android app and It doesn't have an open port that the index.js(backend) can listen or serve to? Commented Jul 15, 2020 at 5:37
  • Your app requires a Socket.IO server. What exactly are you expecting to happen here? Commented Jul 15, 2020 at 5:39
  • 1
    @AnmolVashisht The index.html content is usually packaged inside the app itself. The backend service still runs in node, on server somewhere, not in the cordova app. The service would normally be hosted somewhere where all the people running the android app can access it (like aws) Commented Jul 15, 2020 at 5:44
  • 1
    @AnmolVashisht The Node.js code (index.js) won't run on Android. Commented Jul 15, 2020 at 5:45

1 Answer 1

2

The frontend is basically the html file(s) which will be send to the user. It contains all visuals and is basically a gateway to speak with your backend. In your case it the index.html

The backend processes all incoming http-requests of the user. In your case it handles the socketio requests send by the client. It is the place to perform business logic and to interact with your database.

They could be treated as different projects because they operate on different Layers. The presentation layer and Business-Logic/appication layer. But in the endproduct they are related because the server need to send the html files to the client and the http-requests must match on the server and client side.

Nodejs and the express are a great way to build RESTful Webservices which are just a way to retrieve raw data without any presentation-logic in it. It is the frontends responsibility to display the data accordingly.

Sign up to request clarification or add additional context in comments.

6 Comments

I do understand that and thanks for your time but I am confused by the fact that the backend js file (index.js) is serving the index.html on and listening to it on port 3000 on localhost, but how should I completely seperate the front and backend to the point that they can be hosted even on completely different platforms?
I think i understand now your problem. You need to build a cordova app but you are currently serving the html file with the server and your question is now to outsource the served html file to a cordova app.
How can I serve to the index.html when I convert it to an android app and It doesn't have an open port that the index.js(backend) can listen or serve to?
The index.html doesn't get served. The code will basically establish a socket connection with the server. So the client will send a http request to the server an it will keep the connection alive to transfer data back and forth. The server doesnt need to know about any ports of the client.
The index.js doesn't need any changes (except maybe sending the html file). The Cordova app is the client and will need a server which handles the messages (index.js). The Cordova app need to send a http request to let the sever know that he is a client of the app and that the client want to receive new messages. The Server will process all incoming requests and register them as subscriber of new messages. The only thing that will chnage is that the request send to the server is not a browser like chrome but the cordova app framework.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.