Ask coding questions

← Back to all posts
Why dosent node.js work on my website?
Xenity (9)

both these images ^ are from the html repl

this ^ is from the node.js repl

so, i made a node.js server for my html website, but it says that require is not defined, in another repl, i used the same website but set the language to node.js and it works, except the problem is that it messes up everything.. does anyone know what i can do to either fix this on my html repl or on the node.js repl?

Answered by Coder100 (18057) [earned 5 cycles]
View Answer
Comments
hotnewtop
RoBlockHead (520)

node.JS is supposed to be run from a server, the HTML repl is a static file server, so no code is run before it is sent to the client. If you want to use node.js, you need to use a node.js repl.

Xenity (9)

@RoBlockHead yes i have tried that, but it completely ruins my website (look at the third image on the main post)

RoBlockHead (520)

@Xenity with a node.js repl you need to also serve the files that you want your site to use, your site can't just ask for styles.css if the server doesn't know what to send when it receives that request. You need to use a thing called Static File serving, which is an option available in Express

Coder100 (18057)

make a node repl first:
https://replit.com/l/node

and then I can set up the server for you @Xenity

Xenity (9)

@Coder100 um the link does not work..

Xenity (9)

@Coder100 i already have a node repl, it is the one that works, but breaks the css and formatting: https://replit.com/@Dragonflyy2/Website-nodejs-server-TEST?v=1 (it is a team repl)

Coder100 (18057)

https://replit.com/@Coder100/Website-nodejs-server-TESTaoe-uroeh-uarcoea-uaoe-u

css and static files don't get served to the server. You have to be explicit. @Xenity

Xenity (9)

@Coder100 Nvm u just did what i wanted, thanks :D

Xenity (9)

@Coder100 wait there is a problem.. every page i go to says 404 page not found..

Xenity (9)

@Coder100 nvm i fixed it, Tysm