[英]Setting up a node.js & express project
I am new to javascript and node.js, and I want to create a login and sign-up screen.我是 javascript 和 node.js 的新手,我想创建一个登录和注册屏幕。 I have my HTML and CSS already made.
我的 HTML 和 CSS 已经做好了。 How would I set up my project to render my HTML and start my server?
我将如何设置我的项目来渲染我的 HTML 并启动我的服务器? All I have is
const server = http.createServer
.我所拥有的只是
const server = http.createServer
。 I have looked at tutorials online but I am unable to figure it out.我看过网上的教程,但我无法弄清楚。
Thank you to everyone who helps!感谢所有提供帮助的人!
So you need to set up a project and serve static files (HTML, CSS, JS..).因此,您需要设置一个项目并提供 static 文件(HTML、CSS、JS..)。 In Nodejs, you can do it easily with Express .
在 Nodejs 中,您可以使用Express轻松完成。 For example, you can have this in your
server.js
:例如,您可以在
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.在样板文件中,您不需要 ejs 部分,只需将 index.html 文件放在
/public
文件夹中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.