[英]How to integrate express.js with your website
I built a simple web portfolio page with html, css and javascript. I did it with no framework like angular or react.我用 html、css 和 javascript 构建了一个简单的 web 投资组合页面。我没有像 angular 这样的框架或反应。 I used bootstrap for my footer alone.
我单独为我的页脚使用了 bootstrap。
I used webpack as my bundler and everything is working just perfect but my problem right now is that I want to integrate express.js with my app and I have no idea how to do that, I'm about finishing Server side programming with Nodejs and Express course from Coursera.我使用 webpack 作为我的捆绑器,一切工作都很完美,但我现在的问题是我想将 express.js 与我的应用程序集成,但我不知道该怎么做,我要用 Nodejs 完成服务器端编程,并且Coursera 的快速课程。 I have searched through google but couldn't find what I want.
我已经通过谷歌搜索但找不到我想要的东西。
Install in your backend project folder, in console: npm install express --save安装在您的后端项目文件夹中,在控制台中:npm install express --save
var express = require('express'); var app = express(); // Configure headers y cors app.use((req, res, next) => { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Headers', 'Authorization, X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Allow-Request-Method'); res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE'); res.header('Allow', 'GET, POST, OPTIONS, PUT, DELETE'); next(); }); app.use('/api', project_routes);
If you want to expose your back-end API to your front-end in a simple way, can I suggest using api-mount which is based on express under the hood.如果您想以简单的方式将后端 API 暴露给前端,我可以建议使用基于express under the hood 的api-mount 。
Should you need to customize express , introduce additional middlewares or similar - api-mount allows that.如果您需要自定义express ,引入额外的中间件或类似的 - api-mount允许这样做。
The downside is that your back-end would not be RESTfull and you would learn less about express itself.缺点是您的后端不会是 RESTfull 的,并且您对 express 本身的了解会更少。 Still, try it out - you might like it:)
不过,请尝试一下 - 您可能会喜欢它:)
There are different ways to integrate your backend with your frontend.有多种方法可以将后端与前端集成。 You said you were finishing up a course on Node JS and Express on Coursera so I'll assume you already know how to set up your server and whatnot.
你说你在 Coursera 上完成了关于 Node JS 和 Express 的课程,所以我假设你已经知道如何设置你的服务器等等。
If you're starting out and just want to have your data coming from a database and have it populated in the actual HTML I would take a look at EJS .如果您刚开始,只想让数据来自数据库并将其填充到实际的 HTML 中,我会看一下EJS 。 It allows you to embed data into an HTML page, that way you can generate a finished HTML page with all the data you need.
它允许您将数据嵌入到 HTML 页面中,这样您就可以生成包含所有所需数据的完整 HTML 页面。 This is great for things like SEO.
这对于 SEO 之类的事情非常有用。
Another way is to have some client-side Javascript that queries your backend via a REST API in some way like:另一种方法是让一些客户端 Javascript 通过 REST API 以某种方式查询您的后端,例如:
In Express:在快递中:
async function getSomethingFromDB(req, res) {
const data = await getDataFromDb()
res.send(data)
}
app.get("/something", getSomethingFromDB)
In your frontend JS:在你的前端 JS 中:
async function fetchDataFromBackend() {
const res = await fetch(`/something`)
const data = await res.json()
return data
}
You can call fetchDataFromBackend
however you want and do whatever you need with data
, such as populating your website with this data.您可以随心所欲地调用
fetchDataFromBackend
并使用data
做任何您需要的事情,例如用这些数据填充您的网站。 This approach is great to make interactive applications but has its own problems regarding SEO and I would advise the first method, using this technique once you require AJAX in your website.这种方法非常适合制作交互式应用程序,但在 SEO 方面有其自身的问题,我建议使用第一种方法,一旦您在网站中需要 AJAX 就使用这种技术。
I hope this helps!我希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.