0

I am new to javascript and node.js, and I want to create a login and sign-up screen. I have my HTML and CSS already made. How would I set up my project to render my HTML and start my server? All I have is const server = http.createServer. I have looked at tutorials online but I am unable to figure it out.

Thank you to everyone who helps!

3
  • 1
    Hi, for this specifically take a look at digitalocean.com/community/tutorials/…. I highly recommend you first learn about node js and express before doing this. Commented May 22, 2021 at 18:36
  • Thank you, I will look at that. Commented May 22, 2021 at 18:37
  • 1
    Thank you, this helped me. I was able to render my HTML file. Commented May 22, 2021 at 18:51

1 Answer 1

1

So you need to set up a project and serve static files (HTML, CSS, JS..). In Nodejs, you can do it easily with Express. For example, you can have this in your server.js :

// require libraries
const path = require("path");
const express = require("express");
const ejs = require("ejs");

// initiate express app
const app = express();

// config express app
app.use('/', express.static("public"));// express app serves static files (css, js...) in "public" folder
// to include the stylesheet at public/css/style.css, we use  <link rel="stylesheet" href="/css/style.css">
// to include the script at public/script/script.js, we use <script src="/script/script.js"></script>
// you can put index.html in /public folder

app.listen(5000, function () { console.log("Server is listening on port 5000") });

// your page will be available at http://localhost:5000

And the folder structure is

node_modules
public
  index.html
  css
    style.css
  script
    script.js
server.js

I've created a working project here. The code is self-documented. Feel free to test it :). In the boilerplate, you don't need the ejs part, just put your index.html file in the /public folder.

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

2 Comments

Thank you, this helped me a lot. How did you learn javascript and node? Do you have any articles or tutorials that I could look at?
You can look at freecodecamp.org/learn, w3schools.com/js/default.asp. Nodejs/ javascript has many resources online :) When you're families with the language, try to learn & apply some best practices from here

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.