Node.js sending GET command [duplicate]

0

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

    
asked by anonymous 10.09.2016 / 18:28

1 answer

1
var http = require("http").createServer(servidor);
var io = require("socket.io").listen(http);
var fs = require("fs");

function servidor(req, res){
    if (req.url.indexOf('/') != -1) {
        res.writeHead(200);
        res.end(fs.readFileSync("index.html"));
    }

    if (req.url.indexOf('css/bootstrap.min.css') != -1) {
        res.writeHead(200, {"Content-Type": 'text/css'});
        res.end(fs.readFileSync("css/bootstrap.min.css"));
    }

    if (req.url.indexOf('css/sb-admin.css') != -1) {
        res.writeHead(200, {"Content-Type": 'text/css'});
        res.end(fs.readFileSync("css/sb-admin.css"));
    }

    if (req.url.indexOf('font-awesome/css/font-awesome.min.css') != -1) {
        res.writeHead(200, {"Content-Type": 'text/css'});
        res.end(fs.readFileSync("font-awesome/css/font-awesome.min.css"));
    }
}

io.on("connection", function(socket){
    socket.on('mensagem', function(msg) {
        console.log('Recebido: ' + msg);
    });
});

http.listen(5000, "127.0.0.1", function () {
    var host = http.address().address;
    var port = http.address().port;

    console.log('Exemplo na URL http://%s:%s', host, port);
});
    
10.09.2016 / 20:02