Reputation: 4004
Node.js is running on a Linux server on port 8000.
This is a minimal set up as I am trying to build an application bit-by-bit.
On node.js we have 2 scripts:
// index.js
const express=require("express")
const server_route=require("./routing")
const app=express()
const cors = require("cors");
app.use("/",cors, server_route);
app.listen(8000,()=>{
console.log("Server is Running on the port 8000")
})
Then we have the routing script:
//routing.js
var express = require('express');
var router = express.Router();
router.get('/startRoom?rname=.*&uname=.*', function(req, res, next) {
console.log('Start Room');
res.send('Start a Room');
res.end();
});
router.get("/joinRoom?rname=.*&uname=.*",(req,res,next)=>{
console.log('Join Room');
res.send("Join room page");
res.end();
})
router.get("/sendMesg?rid=.*&uid=.*&msg=.*",(req,res,next)=>{
console.log('Send Mesg');
res.send("Send Message page");
res.end();
})
router.get("/*",(req,res,next)=>{
console.log('Inv Page');
res.send("Invalid page");
res.end();
})
console.log( 'Router : ' + JSON.stringify(router) );
module.exports = router;
On the client the following Javascript is used to call this Node.js application.
<script>
function createRoom( uname, rname ) {
// chat.html?username=Saeed&room=r1
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
console.log('readyState: ' + this.readyState);
console.log('status: ' + this.status);
console.log('responseText: ' + this.responseText);
if (this.readyState == 4 && this.status == 200) {
document.getElementById("room_content").innerHTML += this.responseText;
}
};
var qurl = "http://10.0.0.94:8000/startRoom?rname=" + rname + '&uname=' + uname;
console.log ('qUrl:' + qurl);
xmlhttp.open("GET", qurl, true);
xmlhttp.send();
}
</script>
Test:
<script>createRoom('<?= $usr_name ?>','<?= $room_name ?>');</script>
When I run this script in the browser I get nothing returned.
The XMLHttpRequest properties being printed on teh console lg are:
Property | Value |
---|---|
readyState | 1 |
status | 0 |
Short delay then
Property | Value |
---|---|
readyState | 4 |
status | 0 |
If I try the URL directly in the browser (or curl) it just hangs:
http://10.0.0.94:8000/startRoom?rname=Room1&uname=User2
I expecting/wanting to see the responseText of "Start a Room"
Upvotes: 0
Views: 48
Reputation: 901
should fix if you
// index.js
const express=require("express")
const server_route=require("./routing")
const app=express()
const cors = require("cors");
app.use("/",cors, server_route.Routes);
app.listen(8000,()=>{
console.log("Server is Running on the port 8000")
})
./routing
|--- index.js // create this new file for exporting Routes
|--- routing.js
// index.js
const Routes = require('./routing');
module.exports.Routes = Routes;
//routing.js
...
module.exports = router;
Believe the problem is that require is loading up the file - while app.use
requires that a module or function be provided.
Upvotes: 1