简体   繁体   中英

How to render index from Angular project in Node Project

I have two folders into a principal folder, one folder is called frontend, and had the angular project, and the other is called backend, this folder has a backend and all API. I would switch this project like SSR project, can integrate the angular project into a node project, then when I execute

npm run dev

And node project starts on the respective port, automatically the frontend side starts within.

I tried multiples ways to create a path from one folder to another, but not have success.

服务器 index.js

项目结构项目

If I do this

const indexTest = (path.join(__dirname, `../frontend/src/index.html`))

server.get('/', (req, res) => {
    res.render(indexTest)
})

Appear this error

错误 res.render

But, if I do that

const indexTest = (path.join(__dirname, `../frontend/src/index.html`))

server.get('/', (req, res) => {
    res.sendFile(indexTest)
})

Don't show me any error, but the principal page is blank

空白页

Server firmware code

固件

Server app index.js principal

索引服务器应用

You are trying to render the frontend based on the source, and that will not work. On Angular projects you need to build the code first, so later you can render it.

If you want to do SSR, then you will need to use something like Angular Universal

Another option would be to point the backend to the dist folder, and then run the angular cli in watch mode (scroll till bottom). Like this: ng build --watch --output-path dist

You will need to add this code to your backend.

Probably something like this:

server.use(express.static('../frontend/dist'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM