javascript - nodejs的两点疑问
本文介绍了javascript - nodejs的两点疑问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
1,我直接将html文档返回到浏览器,但是html里面引用了一些本地文档,如外部style,外部js,图片之类的,这些全都访问不到,是否要针对这些单独写路由。
2,如何做到像模板引擎一样,往html文档里面填值。
以上两种都是在不用框架的情况下。
先谢过各位了。
解决方案
需要一个路由
用 es6 的 string template.
example:
const render = (context, ...others) => {
return `
<html>
<body>
<h1>{context.title}</h1>
<p>
{
`
{
if (context.condition) {
<span>${context.something}</span>
} else {
<span>${context.somethingElse}</span>
}
}
`
}
</p>
</body>
</html>
`
}
res.send(render({title: 1, something: 1, ...rest}))
这篇关于javascript - nodejs的两点疑问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文