Hello! I'm having trouble using the GET / Test command in this case, in which it does not return any information.
Node.js Code
var http = require("http").createServer(servidor);
var express = require('express');
var io = require("socket.io").listen(http);
var app = express();
var fs = require("fs");
var recebido;
var contentTypes = {
js: 'text/javascript',
css: 'text/css',
json: 'application/json',
png: 'image/png',
jpg: 'image/png',
wav: 'audio/wav'
};
function servidor(req, res) {
var contentType = 'text/html';
var filePath = '.' + req.url;
if (filePath == './' || filePath == './index.html') filePath = './index.html';
else contentType = contentTypes[req.url.split('.').pop()];
fs.readFile(filePath, function(error, content) {
if (error) {
if (error.code == 'ENOENT') {
fs.readFile('./404.html', function(error, content) {
res.writeHead(200, {
'Content-Type': 'text/html'
});
res.end(content, 'utf-8');
});
} else {
res.writeHead(500);
res.end('Ooops... houve um erro: ' + error.code + ' ..\n');
res.end();
}
} else {
res.writeHead(200, {
'Content-Type': contentType
});
res.end(content, 'utf-8');
}
});
}
io.on("connection", function(socket) {
socket.on('mensagem', function(msg) {
console.log('Recebido: ' + msg);
recebido = msg;
});
});
app.get('/teste', function(req, res) {
res.charset = 'UTF-8'
res.send(recebido);
});
http.listen(5000, "192.168.0.108", function() {
var host = http.address().address;
var port = http.address().port;
console.log('Exemplo na URL http://%s:%s', host, port);
});
When you use the GET / Test command or link , the page loads blank