繁体   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