Node.js in HTML

Question:
How do i get node.js to work in an html project? I have been trying to get node.js to work in html for a few days now and nothing seems to work (that ive found). I found this project on another old replit ask but i cant figure out how @Coder100 did it:

https://repl.it/@Coder100/BraveBetterFlash-aeount-euhuoe-a#public/index.html

all i need to use node.js for is this:

const { MongoClient, ServerApiVersion } = require('mongodb');
const uri = "mongodb+srv://test:CeznoqFJJZSx9e5h@cluster0.9kxt3qm.mongodb.net/?retryWrites=true&w=majority";

// Create a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(uri, {
  serverApi: {
    version: ServerApiVersion.v1,
    strict: true,
    deprecationErrors: true,
  }
});

async function run() {
  try {
    // Connect the client to the server	(optional starting in v4.7)
    await client.connect();
    // Send a ping to confirm a successful connection
    await client.db("admin").command({ ping: 1 });
    console.log("Pinged your deployment. You successfully connected to MongoDB!");
  } finally {
    // Ensures that the client will close when you finish/error
    await client.close();
  }
}
run().catch(console.dir);

In Coder100’s repl, they created an express server in nodejs as the backend, and made it host an html page.

3 Likes

not gonna lie i dont know what the heck you said but can you tell me how to do it any ways

1 Like

I am confused what you mean by making NodeJS in HTML. You use NodeJS to make a server to host HTML. You can use HTML in NodeJS but not NodeJS in HTML. Also AFAIK you cannot use NodeJS on an HTML/CSS/JS Repl template because those templates are special. Luckily you used the NodeJS template. Can you please rephrase your question?

3 Likes

To create an HTML application with the Node.js backend, use Node.js template with HTML, CSS files.

4 Likes

all i really need to do is be able to use mongodb atlas in repl, so if you could help me with it, thank you

1 Like

What does that have to do with HTML? Here’s the Docs on MongoDB on Replit.

4 Likes

well i need to me able to use the information stored on mongodb’s server for the website

1 Like

What template do you use to create a website?

1 Like

The html/css/js template

1 Like

Why are you trying to make it in an HTML/CSS/JS Repl? Just create a Node.js Repl like Coder100 did.

3 Likes

I would highly recommend not letting people send in code for XSS reasons.

4 Likes

All you have to do is fork

https://replit.com/@Replit/Nodejs

and then put in the Node.js you need, put in the HTML and run the repl. A regular HTML/CSS/JS repl can’t run Nodejs at all, it has no server.

Edit: the javascript alone won’t be enough btw you need to start a server with Express, https://expressjs.com/, and modify the code to host the website

4 Likes

why does the error that pops up disappear right way in the console and it starts to loop showing the same text than an error i cant read

https://replit.com/@OwenStritz/nodejs?v=1

A bug with Replit, crashloops are common and usually happen when you’re trying to stop a Repl, while the client/service is still trying to run the Repl.

4 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.