I'm creating an example of node.js
for uploading files and store form. The problem is my css
and js
files not work when I access from localhost
in any port. I use port 3000, when I open the file index.html
in browser is ok, but when I follow localhost:3000
or any port, the port isn't the problem and css
files do not work. So my codes are:
The HTML
:
<!DOCTYPE html> <html> <head> <title>Video Uploader</title> <meta charset="utf-8"> <link href="/css/style.css" rel="stylesheet" type="text/css"> <script type="text/javascript" src="/javascript/formstorage.js"></script> <script src="/javascript/jquery-ui.js"></script> </head> <body> <form> <div class="cont fl"> <h1>File's Uploader:</h1> <div class="fl form"> <div> <h1> Your Name: </h1> <input type="text" class="inps" id="nome"> </div> <div> <h1> e-mail: </h1> <input type="text" class="inps" id="email"> </div> <div class="bt"> <div class="fl"> <input type="button" value="Record" id="rec"> </div> <div class="fr"> <input type="button" value="Upload" id="file"> </div> </div> <input type="button" value="Confirm" id="sub"> </div> <!-- fim do formulario --> <div class=" fr" id="contentv"> </div> </div> </form> </body> </html>
The js
for open server:
var express = require('express'); var app = express(); var path = require('path'); var formidable = require('formidable'); var fs = require('fs'); app.use(express.static(path.join(__dirname, 'public'))); app.get('/', function(req, res){ res.sendFile(path.join(__dirname, 'index.html')); }); var server = app.listen(3000, function(){ console.log('Server listening on port 3000'); });
Remove 'public' in path. cf) /css/style.css
Cause of this line ( app.use(express.static(path.join(__dirname, 'PATH')));
),
the PATH/FILE
's absolute URL
is just /FILE
.
tanks for help me. I do know but I modify this code several times, and not work.. Now what I do: I create de folder public/css/style.css and add the line app.use() with 'public' like 김재원 say in my Node file. And in link of html I write "/css/style.css". Before still isn't work. I try and try and try but isn't work. NOW is working... I do know what's happening here, but the same thing now is working lol.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.