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>