I have node v7.4.0 installed on my remote server. I've installed the latest version of express which is 4.14.0 and I've set up index.js in my public_html. index.js is a copy of the official test online:
const express = require('express')
const app = express()
const port = 3000
app.get('/', (request, response) => {
response.send('Hello from Express!')
})
app.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
})
When I run node index.js while in public_html, I get the expected results: server is listening on 3000. When I go to my server's domain name address or IP address on port 3000, I just get that it is "Connecting" and then it fails saying it took too long to respond (plus no response in the command line). What can I look into to fix this?
Looks like nothing is wrong with your code. It may be a firewall/antivirus; additionally, Try using another browser like Firefox. Make sure to use the loopback ip 127.0.0.1:3000 or localhost:3000 (same thing).
For me, it was because of the proxy. After disabling proxy for intranet, it worked fine.
For me it was because of the firewall settings. I added private network access and then it worked fine.
Related
I am just learning webdev and want to try to make a multiplayer game using Express and socket.io
I can make a server with socket.io in it which listens. That part works fine.
However when I try to connect a client, this only works if I let the HTML file with the following in it be served by the server like follows:
Server code:
const express = require('express')
const app = express()
const http = require('http')
const server = http.createServer(app)
const { Server } = require('socket.io')
const io = new Server(server)
const port = 3000
io.on('connection', (sock) => {
console.log('client connected')
})
// This seems to be necessary, but I don't want it to be!!!
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html')
})
server.listen(port, () => {
console.log(`Server listening on port ${port}`)
})
This index.html has the following at the bottom:
<script src="/socket.io/socket.io.js"></script>
<script>const socket = io()</script>
However I want to keep all my frontend code seperate from the server. I made a seperate repository for the frontend and backend. I want the frontend to contain all the UI logic and use only data calls (AJAX) to get Json data from the server. So I want to put this index.html file in my frontend code only.
Yet if I do this the connection doesn't work.
I can start the server fine.
I open index.html from WebStorm which also creates a server for this which I configured to also listen to port 3000
Yet it cannot find /socket.io/socket.io.js and I get the following error in the console.
It also doesn't work if WebStorm runs on a different port.
The resource from “http://localhost:3000/socket.io/socket.io.js” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff).
How can I keep this html in my client repo only and still work with socket.io, or is this not possible?
You can't have multiple servers listening on the same port. Run the servers on different ports and either:
Have a reverse proxy forwarding requests to your Socket.io server (which needs special handling) and your front end server or
Put an absolute URL in the script src and configure CORS.
I made a index.html file and index.js and server.js.
Inside server.js I have written the following code:
const express = require("express");
const path = require("path" );
const app = express();
app.use("/static",express.static(path.resolve(__dirname, "frontend", "static")));
app.get("/*", (req,res)=>{
res.sendFile(path.resolve(__dirname,"frontend","index.html"));
});
app.listen(process.env.PORT || 5060, ()=> console.log("Server Running..."));
No error is shown by the vs code and it is working fine in terminal but giving error msg when I am trying to load the application on chrome browser with localhost:5060 url with ERR_UNSAFE_PORT error. Please suggest some way to resolve this.
So, there are a bunch of ports which are considered unsafe by chrome browser which includes 5060 which you were specifying earlier. That's why earlier you were getting "ERR_UNSAFE_PORT" error when you were trying to load localhost:5060.
From the program perspective, there isn't anything wrong. And at first glance everything will look okay. The problem starts when chrome identifies the port and declares it unsafe. This is done by the browser to prevent XSRF so, that someone doesn't use chrome as a proxy to attack your services.
How do you know which are the ports we are not supposed to use? Refer at this link which provides a list of blocked ports on chrome browser - https://chromium.googlesource.com/chromium/src.git/+/refs/heads/main/net/base/port_util.cc
Final program would look like:
const express = require("express");
const path = require("path" );
const app = express();
app.use("/static",express.static(path.resolve(__dirname, "frontend", "static")));
app.get("/*", (req,res)=>{
res.sendFile(path.resolve(__dirname,"frontend","index.html"));
});
app.listen(process.env.PORT || 3001, ()=> console.log("Server Running..."));
I'm having an issue with my express server with an email service I was attempting to set up. After troubleshooting I decided to boil it down and attempt see if the issue would replicate with a simple 'hello world' example, which it did. No routes will be work correctly each request, whether done by a js frontend, postman, or just in a chrome browser will work. Each request will just 'spin' until it returns a 'No Data Received ERR_EMPTY_RESPONSE' error.
I've tried reinstalling the express dependency, reinstalling node itself, different browsers. The code is attached, any help would be appreciated.
const express = require('express');
const cors = require('cors');
const app = express();
let port = 3000;
app.use(cors);
app.get('/testroute', (req, res) => {
console.log('route hit');
res.send('test success');
});
app.listen(port, () => {
console.log('server started on port: ' + port);
});
Change this:
app.use(cors);
to this:
app.use(cors());
Your server was hanging because you were passing cors as middleware. But, cors by itself is not middleware. When Express called it, it never sent a response or called next() to continue routing, so therefore the client was just left hanging forever waiting for a response. Instead, the cors library is designed to that you call it as in cors() to get back a middleware function that you then pass to Express.
This is fully documented in the cors library documentation.
I used socket.io on localhost:3000 for several tests. Now i tried getting it to work online for an hour but I’m making no progress.
server.js
const http = require('http').createServer()
const io = require('socket.io')(http)
io.on('connection' (socket) => {
socket.on('start_session', (data) => {
console.log('hey there')
})
})
app.js
const socket = io('http://localhost:3000')
socket.emit('start_session', session_id)
The server.js is running on a server, it’s in the same folder as my index.html and app.js, on my own device it’s working perfectly fine
What am I missing? Can this even be archived with using localhost? I searched for alternatives but it’s always localhost.
Thanks in advance
The path to the socket.io.js file is also not working, how do I find that out? Tried the whereis command, but that didn’t help.
I've homework for Client Server Programming course and it want me to create Multi-Client Web Servers. But the rule is web server must implement HTTP version 1.0 protocol, where separated HTTP requests are sent for each component of the Web Page.
Unfortunately, I'm just know to work with Node.js. I know C, but it long times (around 10 years ago) and only do very basic programming such as arithmetic operation, work with string and arrays, not OOP in C.
So the question, how to create web server with HTTP/1.0 protocol in Node.js?
Currently, I have node v.10.15.1 installed on my laptop (using macos). I've tried with http and net module but can't find how to configure the protocol to use HTTP/1.0
We need to require the http module first and bind our server to the port which we listen on.
Inside the index.js :
// content of index.js
const http = require('http')
const port = 3000
const requestHandler = (request, response) => {
console.log(request.url)
response.end('Hello Its Your Node.js Server!')
}
const server = http.createServer(requestHandler)
server.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
})
So lets start it with:
$ node index.js