DanyDC
DanyDC

Reputation: 73

Use local files in my node.js/socket.io project

I have a node.js/socket.io project. I'm using Bootstrap in my pages and I display images dynamically on my pages. The problem is that I can't use the images and the Bootstrap files. Node.js/socket.io doesn't recognize the links... I have solved the Bootstrap problem by uploading all Bootstrap files on a distant server and it works! But I can't use local files like my images.

How can I "load" an image folder which I can use the images from it?

Here is my server.js :

var http = require('http').createServer(createServer);
var fs = require('fs');
var url = require('url');
var nStatic = require('node-static');
var express = require('express');
var app = express();

function createServer(req, res) {
    var path = url.parse(req.url).pathname;
    var fsCallback = function(error, data) {
        if(error) throw error;

    res.writeHead(200);
    res.write(data);
    res.end();
}

switch(path) {
    case '/galerie.php':
        doc = fs.readFile(__dirname + '/galerie.php', fsCallback);
    break;
    default:
        doc = fs.readFile(__dirname + '/index.php', fsCallback);
    break;
}

var io = require('socket.io').listen(http);

io.sockets.on('connection', function (socket, pseudo) {
...
});
http.listen(8080);

This works :

<script src="http://website.ch/LivreOr/js/download.js"></script>

But this doesn't work :

<img src="../LivreOr/img/img.png">

Upvotes: 0

Views: 756

Answers (1)

DanyDC
DanyDC

Reputation: 73

I have solved my problem. Here is what I have changed :

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var fs = require('fs');

app.use(express.static(__dirname + '/public'));

app.get('/galerie', function(req, res) {
    res.sendFile(__dirname + '/galerie.html');
});

app.get('/', function(req, res) {
    res.sendFile(__dirname + '/index.html');
});
//var io = require('socket.io').listen(http);

io.sockets.on('connection', function (socket, pseudo) {
...
});
server.listen(8080);

Upvotes: 2

Related Questions