簡體   English   中英

設置 node.js & express 項目

[英]Setting up a node.js & express project

我是 javascript 和 node.js 的新手,我想創建一個登錄和注冊屏幕。 我的 HTML 和 CSS 已經做好了。 我將如何設置我的項目來渲染我的 HTML 並啟動我的服務器? 我所擁有的只是const server = http.createServer 我看過網上的教程,但我無法弄清楚。

感謝所有提供幫助的人!

因此,您需要設置一個項目並提供 static 文件(HTML、CSS、JS..)。 在 Nodejs 中,您可以使用Express輕松完成。 例如,您可以在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

文件夾結構是

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

我在這里創建了一個工作項目。 代碼是自記錄的。 隨意測試它:)。 在樣板文件中,您不需要 ejs 部分,只需將 index.html 文件放在/public文件夾中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM