-1

I'm trying to do API in javascript for another users. I want to create a chat in realtime in Node.js with socket.io but I want to give the opportunity loading this API in common HTML through javascript. For example, if somebody copy and paste simple js script into your html, then chat is loaded.

My app is running in node on port 8080 and my other page is html on port 80. How can I put node.js in other page?

below is my chat.js

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

var cors = require('cors');

app.use(cors());
app.options('*', cors());

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

});

 io.on('connection', function(socket){

 console.log('a user connected');

   socket.on('chat message', function(msg){
   console.log('the user wrote:' +msg);
   io.emit('chat message', msg, id);

   });

 });

 http.listen(8080, function(){
  console.log('listening on *:8080');
 });

In this way I try to load:

<script>
  $.ajax({

      xhrFields: {
          withCredentials: true
      },
      dataType: "html",
      url: "http://127.0.0.1:8080/index.js"

   }).done(function(data){

   // next function here;

   });
</script>
SeaDog
  • 645
  • 1
  • 9
  • 32
  • Have you tried putting `:8080` in the URL? – Quentin Nov 08 '16 at 14:51
  • Yes, I can normally running node.js on 8080 port. But I want a simple website running on port 80 and include this chat as API for example as javascript. I can not serve in the node. – SeaDog Nov 08 '16 at 15:00
  • … so you have an HTML page on port 80 and load the chat with a URL that has :8080 on it. What's wrong with that? – Quentin Nov 08 '16 at 15:04
  • Or are you trying to run a Socket.IO server without running a server which supports it? – Quentin Nov 08 '16 at 15:12
  • no, at the beginning I run node. – SeaDog Nov 08 '16 at 19:23
  • "My app is running in node on port 8080" — Umm…`http.listen(3000` – Quentin Nov 09 '16 at 08:35
  • Why are you trying to use `$.ajax`? That isn't how you normally access Socket.IO – Quentin Nov 09 '16 at 08:35
  • I know, I corrected. In my origin project I use port 3000 – SeaDog Nov 09 '16 at 08:36
  • How can I access to socket? – SeaDog Nov 09 '16 at 08:38
  • http://socket.io/get-started/chat/ – Quentin Nov 09 '16 at 08:39
  • I know this tutorial. When I am doing this step-by-step all is great works but I want to do in form API. Look... I have a database. I share my app on my website for example http://chatforyou.com Now you create an account and you get simply javascript with your ID. Then paste this code on your site and you happy a chat on your site. I can't include this html/socket whatever on other domain. Sorry, but I'm beginner in node. – SeaDog Nov 09 '16 at 08:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127696/discussion-between-seadog-and-quentin). – SeaDog Nov 09 '16 at 08:52

1 Answers1

1

You don't "include node.js", what you need to do is include the script that connects to the server via socket.io (8080 server).

This post may be useful for you http server and web sockets from separate servers.

On the other hand, you can include client side javascript from the 8080 server in order to make the chat run correctly.

Edited for more details.

In the server running on 8080 you can create a server with Socket.io and Express, there you can have the server side logic of your chat and serve the JavaScript file with the client side logic. Let's say, the client JavaScript file is located in http://localhost:8080/yourchat.js (of course, in some sort of real world you may have to generate this file in order to emit data to the correct users)

In the server running on 80 you'll have your div element and a script tag with this source: http://localhost:8080/yourchat.js. Using this pattern you sould be able to embed the chat

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
  • If my app is running on localhost:8080, how can I load this to other html on port 80? I would like only that this app works in div on my website. – SeaDog Nov 08 '16 at 19:19
  • I updated my question. Well, I run my chat by node chat.js but html is in index.html. On another domain I trying load to div this html but I can't. When I load only domain just like: http://127.0.0.1:8080 I receive in console log: "Access-Controll-Allow-Origin". When I trying load http://127.0.0.1:8080/chat.js I receive error 404. – SeaDog Nov 09 '16 at 08:34