0

I have this code that allows me to open a HTML page from specific folder, if I use server.js to open that HTMLpage so the page it is generating with all the css and jquery files but if I try to move the get statement to the routes folder then the page is generated but without any css and jquery files and I don't know why !

what I did in the server.js for the generation of the HTML page is below which is working perfectly :

const folderPath = __dirname + '/public/AppTemplate/src'
app.use(express.static(folderPath))
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', function (req, res) {
  res.sendFile(path.join(__dirname + '/public/AppTemplate/src/index.html'));
});

but what I'm trying now is to get the html page from routes.js :

step 1 : I implemented this statement in server.js

app.use('/users', require('./backend/routes/profile.routes.js'));

step2 :I tried this statement in routes.js with simple modification :D :

router.get('/profile', function (req, res) {
  const dirname = __dirname;
  console.log(dirname)
  const newpath = dirname.length - 14;
  const newP = dirname.substring(newpath, dirname.lastIndexOf("/"));
  console.log(newP);
res.sendFile(path.join(newP+ '/public/AppTemplate/src/02-ProfilePage.html'));
});

the step 2 is working but I couldn't get all the associated files (jquery css ...) which are located in

/public/AppTemplate/src

the image of the output is below : enter image description here

hope I mentioned everything, Best Regards,

13
  • May you also show us the code in 02-ProfilePage.html1? Commented Apr 21, 2020 at 10:40
  • @AvivLo , this is the html Page paths for css and Jquery ibb.co/2Frhy1B ibb.co/Svz0WF1 but why the Index HTML is working ? Commented Apr 21, 2020 at 10:45
  • When you say index.html is working are you referring to the HTML part or the script part as well? Commented Apr 21, 2020 at 10:52
  • So what's your path for all the css and js files? Are all the resources under /public/AppTemplate/src? Commented Apr 21, 2020 at 10:53
  • Yes they are all under /public/AppTemplate/src see images ibb.co/grCfdq1 but I Noticed an error in network console see this image ibb.co/yN86yzK Commented Apr 21, 2020 at 11:00

1 Answer 1

1

It's because of the content in the 02-ProfilePage.html has an incorrect path.

Check the path in the script tags. If there is a slash it means that it's already in the /public/AppTemplate/src which you specified.

For example, /js/file.js will actually point to /public/AppTemplate/src/js/file.js

Perhaps try adding a / in front of your path in the script tag.

Example:

/css/x/y/z/ instead of css/x/y/z

You will have to append a / to all the routes in your script/link tag to be able to successfully load the local resources.

You can use the find and replace functionality in your code editor or IDE to speed up the process if possible.

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

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.