'NodeJS + Express + Socket.IO : CANNOT GET ERROR
I'm programming chat app using socket.io . Chatting works well but trying to get 'http://myurl/upload', it give me the error"cannot GET//uplad". What is the problem. I'm sorry that my English is so bad. but please help me. Here's my code.
const express = require('express');
const { createServer } = require('http');
const { Server } = require('socket.io');
const hostname = "127.0.0.1";
const app = express();
app.use(express.static('public')); //it is to bring images. it works well.
app.get('/upload',(req,res,next)=>{
res.send('hello');
}); //it doesn't work...
const httpServer = createServer(app);
const io = new Server(httpServer);
io.on('connection',(socket)=>{
... //skip
}
httpServer.listen(3000,hostname);
console.log("Running at 3000");
Solution 1:[1]
I was able to get your code to work.
There is one typo: you must close the io.on()
function call:
io.on('connection', (socket) => {
// ... //skip
}) // <-- needs closing ')'
You do no need the hostname
param and you should pass a function callback to .listen:
httpServer.listen(3000, () => {
console.log("Running at 3000");
});
Finally, make sure you requesting http://localhost:3000/upload If you request http://localhost:3000/ you will get
Cannot GET /
Because you only mounted the /upload endpoint in express (not the / endpoint)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Andrew |