[英]How would I incorporate a bootstrap css stylesheet into my express/node js?
So at the moment, the approach I am taking is I'm using this code to retrieve my html file.所以目前,我采用的方法是使用此代码检索我的 html 文件。
app.get("/", function (request, response) {
response.sendFile(__dirname + "/public_html/index.html");
})
The html file is a form with method="post" and a button to submit the form. html 文件是一个带有 method="post" 的表单和一个提交表单的按钮。 I have imported the relevant css code here.我已经在这里导入了相关的 css 代码。
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
Back on the js side I have use this code to collect the values from the html form when it is submitted.回到 js 端,我在提交时使用此代码从 html 表单中收集值。
app.post("/survey", function (request, response, next) {
let firstname = request.body.fname;
let surname = request.body.sname;
etc...
Then here is the part where I'm not sure whether or not I'm taking the wrong approach or not.然后这是我不确定我是否采取错误方法的部分。
I use some response.write() methods to display the form results.我使用一些 response.write() 方法来显示表单结果。
response.write('<h1>SURVEY REPORT</h1>');
response.write('<dl><dd><strong>Firstname entered: </strong>' + firstname + '</dd>');
response.write('<dd><strong>Surname entered: </strong>' + surname + '</dd>');
etc...
While this does kind of work, it doesn't seem to implement the css stylesheet at all.虽然这确实有效,但它似乎根本没有实现 css 样式表。 How would I get the css stylesheet to work with the output?如何让 css 样式表与 output 一起使用?
I have also tried creating a sql database in memory via sqlite3 and store the values there, but now I'm stuck with how I would use that table to output into an html format. I have also tried creating a sql database in memory via sqlite3 and store the values there, but now I'm stuck with how I would use that table to output into an html format.
It is not a better way to do You can you Handlebars or ejs.这不是更好的方法你可以使用Handlebars或 ejs。
Add this line on app.js在 app.js 上添加这一行
app.set('view engine', 'hbs');
Then you can response to the request by:-然后您可以通过以下方式响应请求:-
res.render('/filepath/index.hbs', {write variables for the file as json})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.