How to serve simple static files along with html in NodeJS?How can I upload files asynchronously?How do I include a JavaScript file in another JavaScript file?How to create an HTML button that acts like a link?How can I update NodeJS and NPM to the next versions?How to serve static files in FlaskWhat's the difference between app.use and app.get with express.static?how to access node_module directory which was out side the static content serving folderCode inside get not running after using express.staticHow to --watch uncompressed files (without Webpack)Express File Serving and Static Files

Multi tool use
Find the 3D region containing the origin bounded by given planes
Why could the Lunar Ascent Engine be used only once?
Can I have a delimited macro with a literal # in the parameter text?
How do we explain the use of a software on a math paper?
Why didn't Daenerys' advisers suggest assassinating Cersei?
Why are stats in Angband written as 18/** instead of 19, 20...?
Why should one apply for UK visa before other visas, on a multi-destination European holiday?
Why were early aviators' trousers flared at the thigh?
Why did Nick Fury not hesitate in blowing up the plane he thought was carrying a nuke?
Why favour the standard WP loop over iterating over (new WP_Query())->get_posts()?
What city and town structures are important in a low fantasy medieval world?
How could the B-29 bomber back up under its own power?
Parse a C++14 integer literal
How to safely discharge oneself
In how many ways can we partition a set into smaller subsets so the sum of the numbers in each subset is equal?
In Dutch history two people are referred to as "William III"; are there any more cases where this happens?
Have I found a major security issue with login
Is it possible to view all the attribute data in QGIS
Isn't Kirchhoff's junction law a violation of conservation of charge?
How can sister protect herself from impulse purchases with a credit card?
Does ratifying USMCA imply a (stealth) ratification of UNCLOS?
Working hours and productivity expectations for game artists and programmers
Warped chessboard
Bash Read: Reading comma separated list, last element is missed
How to serve simple static files along with html in NodeJS?
How can I upload files asynchronously?How do I include a JavaScript file in another JavaScript file?How to create an HTML button that acts like a link?How can I update NodeJS and NPM to the next versions?How to serve static files in FlaskWhat's the difference between app.use and app.get with express.static?how to access node_module directory which was out side the static content serving folderCode inside get not running after using express.staticHow to --watch uncompressed files (without Webpack)Express File Serving and Static Files
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm trying to send an HTML file with its CSS and JS files in NodeJS using express.static()
but it doesn't work.
I've done the same thing as in some videos but I don't know where the problem is. And also when I open the index.html
without the server, it display the styles.
This is my app.js(server):
app.use(express.static('public'));
app.get("/", (req, res)=>
res.sendFile(`$__dirname/index.html`);
);
HTML:
<head>
<link href="./public/styles.css" rel="stylesheet" type="text/css"/>
<script src="./public/jQuery.js"></script>
<script src="./public/main.js"></script>
</head>
My public folder:
-node_modules
-public
jQuery.js
main.js
styles.css
app.js
index.html
package.json
package-lock.json
Thanks in advance.
javascript html node.js express static-files
add a comment |
I'm trying to send an HTML file with its CSS and JS files in NodeJS using express.static()
but it doesn't work.
I've done the same thing as in some videos but I don't know where the problem is. And also when I open the index.html
without the server, it display the styles.
This is my app.js(server):
app.use(express.static('public'));
app.get("/", (req, res)=>
res.sendFile(`$__dirname/index.html`);
);
HTML:
<head>
<link href="./public/styles.css" rel="stylesheet" type="text/css"/>
<script src="./public/jQuery.js"></script>
<script src="./public/main.js"></script>
</head>
My public folder:
-node_modules
-public
jQuery.js
main.js
styles.css
app.js
index.html
package.json
package-lock.json
Thanks in advance.
javascript html node.js express static-files
add a comment |
I'm trying to send an HTML file with its CSS and JS files in NodeJS using express.static()
but it doesn't work.
I've done the same thing as in some videos but I don't know where the problem is. And also when I open the index.html
without the server, it display the styles.
This is my app.js(server):
app.use(express.static('public'));
app.get("/", (req, res)=>
res.sendFile(`$__dirname/index.html`);
);
HTML:
<head>
<link href="./public/styles.css" rel="stylesheet" type="text/css"/>
<script src="./public/jQuery.js"></script>
<script src="./public/main.js"></script>
</head>
My public folder:
-node_modules
-public
jQuery.js
main.js
styles.css
app.js
index.html
package.json
package-lock.json
Thanks in advance.
javascript html node.js express static-files
I'm trying to send an HTML file with its CSS and JS files in NodeJS using express.static()
but it doesn't work.
I've done the same thing as in some videos but I don't know where the problem is. And also when I open the index.html
without the server, it display the styles.
This is my app.js(server):
app.use(express.static('public'));
app.get("/", (req, res)=>
res.sendFile(`$__dirname/index.html`);
);
HTML:
<head>
<link href="./public/styles.css" rel="stylesheet" type="text/css"/>
<script src="./public/jQuery.js"></script>
<script src="./public/main.js"></script>
</head>
My public folder:
-node_modules
-public
jQuery.js
main.js
styles.css
app.js
index.html
package.json
package-lock.json
Thanks in advance.
javascript html node.js express static-files
javascript html node.js express static-files
edited Mar 23 at 18:40


alditis
2,76933154
2,76933154
asked Mar 23 at 17:49


Eye PatchEye Patch
707
707
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
In app.use(express.static('public'));
, the string 'public'
refers to the directory where the module should search for static files, not the URL path which it should use for the search.
src="/jQuery.js"
will map to the public/jQuery.js
file.
To map URLs starting with /public/
to the public
directory, you need to specify that as the first argument to use:
app.use('/public', express.static('public'));
And then src="/public/jQuery.js"
will map to the public/jQuery.js
file.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55316675%2fhow-to-serve-simple-static-files-along-with-html-in-nodejs%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In app.use(express.static('public'));
, the string 'public'
refers to the directory where the module should search for static files, not the URL path which it should use for the search.
src="/jQuery.js"
will map to the public/jQuery.js
file.
To map URLs starting with /public/
to the public
directory, you need to specify that as the first argument to use:
app.use('/public', express.static('public'));
And then src="/public/jQuery.js"
will map to the public/jQuery.js
file.
add a comment |
In app.use(express.static('public'));
, the string 'public'
refers to the directory where the module should search for static files, not the URL path which it should use for the search.
src="/jQuery.js"
will map to the public/jQuery.js
file.
To map URLs starting with /public/
to the public
directory, you need to specify that as the first argument to use:
app.use('/public', express.static('public'));
And then src="/public/jQuery.js"
will map to the public/jQuery.js
file.
add a comment |
In app.use(express.static('public'));
, the string 'public'
refers to the directory where the module should search for static files, not the URL path which it should use for the search.
src="/jQuery.js"
will map to the public/jQuery.js
file.
To map URLs starting with /public/
to the public
directory, you need to specify that as the first argument to use:
app.use('/public', express.static('public'));
And then src="/public/jQuery.js"
will map to the public/jQuery.js
file.
In app.use(express.static('public'));
, the string 'public'
refers to the directory where the module should search for static files, not the URL path which it should use for the search.
src="/jQuery.js"
will map to the public/jQuery.js
file.
To map URLs starting with /public/
to the public
directory, you need to specify that as the first argument to use:
app.use('/public', express.static('public'));
And then src="/public/jQuery.js"
will map to the public/jQuery.js
file.
answered Mar 23 at 17:54
QuentinQuentin
665k749031065
665k749031065
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55316675%2fhow-to-serve-simple-static-files-along-with-html-in-nodejs%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Ev0KUP zq UFWOWk6I,nfhwzSd,ePN,12Jn az,JQO S 4Qx 2R0U3D2AiIxhsegsRSTY,dJy3FVo