Jessica Chambers
Jessica Chambers

Reputation: 1316

How to call files from a page in nodeJS

I'm running a nodeJS server with the following structure:

├── init.js
├── Integration
│   └── [libraries and stuff]
├── package.json
└── views
    ├── 3Dmodel.html
    ├── 404.html
    ├── index.html
    └── models
        └── damaliscus_korrigum.ply

In init.js (file launched to launch the server) I have:

createServer: function(){
        var express = require("express");
        var app = express();
        var http = require('http');
        var server = http.createServer(app);
        var router = express.Router();
        var path = __dirname + '/views/';

        app.use(favicon(__dirname + '/images/logo.png'));

        router.use(function (req,res,next) {
        console.log("/" + req.method);
        next();
        });

        router.get("/",function(req,res){
        res.sendFile(path + "index.html");
        });

        router.get("/3Dmodel",function(req,res){
        res.sendFile(path + "3Dmodel.html");
        }); //same for all pages
}

I want to call the file /views/models/damaliscus_korrigum.ply in /views/3Dmodel.html here:

var loader = new THREE.PLYLoader();

loader.load( 'damaliscus_korrigum.ply', function ( geometry ) { ... }

but what path should I use to call it? /models/damaliscus_korrigum.ply hasn't worked, nor has models/damaliscus_korrigum.ply or ./models/damaliscus_korrigum.ply.

Upvotes: 1

Views: 40

Answers (1)

Jonas Wilms
Jonas Wilms

Reputation: 138447

First of all you have to serve the models from your server (so that the client can request it), for serving a static directory Express.static is a good choice:

 app.use("/models/", express.static(path + "/models/"));

Now your client can send requests that get answered.

Upvotes: 2

Related Questions