javascript - Resolving dependencies in express server -
i having trouble resolving dependencies on express server.
here project structure
calculator --dist ----app -------calculator.js -------server.js --node_modules --src ----app --------calculator.js --------server.js ----public --------calculator.css --------calculator.html ----.babelrc ----.gitignore ----package.json
here server.js code
const express = require('express'); const app = express(); const path = require('path'); app.get('/', (req, res) => { res.sendfile(path.resolve('./src/public/calculator.html')); }); app.use(express.static(__dirname + '../../src/app/public')); app.use(express.static(__dirname + './')); app.listen(3000, () => { console.log(__dirname); console.log("listening on port 3000"); });
the reason have dist folder , src folder because compiling js es6 src folder es5 within app folder using babel.
however, when launch node server, html not able load css file , js file. using these paths load each respectively calculator.html file
<link rel="stylesheet" type="text/css" href="./calculator.css"> <script type="text/javascript" src="./calculator.js"></script>
i sure missing way files served on localhost. appreciate error being pointed out.
it looks on line 9 in server.js
:
app.use(express.static(__dirname + '../../src/app/public'));
you're going app
folder src
, root, down src
, app
, , trying go down public
. project structure diagram, public
isn't inside app
rather beside it. you'd want more app.use(express.static(__dirname + '../public'));
in addition, i'd recommend using path
module that's built node. it'll make sure don't have mistakes in path creation.
edit: sorry, incorrect. didn't see transpiling server code well. you'll want use these lines, should fix both:
app.use(express.static(__dirname + '../../src/public')); app.use(express.static(__dirname));
this assuming run server.js
file present in dist/app
, not 1 in src/app
.
Comments
Post a Comment