1

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');
});

6
  • Just remove public part from style and script path. Commented Sep 19, 2016 at 5:58
  • this is a folder name, i have public, inside of public folder have css and javascript folders. But I gonna try remove the folders of inside public folder. Commented Sep 19, 2016 at 6:00
  • still does not work. Commented Sep 19, 2016 at 6:03
  • Try to add / at beginning of the paths. Commented Sep 19, 2016 at 6:04
  • I suggest you not to use node js as your entire web server. Commented Sep 19, 2016 at 6:33

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I do this but still not working.. I add this line "app.use[...]" and insert '/'...
just remove public in your html files. like this. <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>
and DO NOT modify app.use(express.static(path.join(__dirname, 'public')));
I do this but still isn'it work.. This is my problem.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.