Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy doesn't work static in Express?
    primarykey
    data
    text
    <p>I have just started to learn <code>nodejs</code> and faced the problem that in <code>Express</code> framework i can't load simple HTML files. </p> <p>My projects structure is like this:</p> <p><img src="https://i.stack.imgur.com/zcASh.png" alt="structure"></p> <p>In assets are two extra folders: <code>js</code> and <code>css</code> which contains files. My <code>server.js</code> file looks like this:</p> <pre><code>module.exports.start = function(config) { var express = require(config.MODULES_DIR + 'express'), app = express(), http = require('http'), server = http.createServer(app), io = require(config.MODULES_DIR + 'socket.io').listen(server), fs = require('fs'), path = require('path'); server.listen(config.APP_PORT); // Setting static files dir to load it automatically app.use(express.static(config.ROOT_DIR + '/assets/')); console.log(config.ROOT_DIR + '/assets/'); app.get('/', function (req, res) { res.sendfile(dir + 'index.html'); }); } </code></pre> <p><code>config.ROOT_DIR + '/assets/'</code> returns <code>/Sites/node_project/assets/</code> which is the exact location of files. To be 100% sure i run <code>ls /Sites/node_project/assets/</code> and it returns <code>css js</code>, but still when i open <code>http://192.168.1.109:4935</code> i get only <code>index.html</code> with no <code>js</code> or <code>css</code>… If i try to open directly some file, for example css i get <code>Cannot GET /assets/css/jquery.mobile.css</code>…</p> <p>What i'm doing wrong?</p> <p><strong>UPDATE</strong></p> <p>Found the solution, it should be like <code>app.use('/assets', express.static(config.ROOT_DIR + '/assets/'));</code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload