Reputation: 908
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!
Upvotes: 0
Views: 340
Reputation: 5411
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.
Upvotes: 1