Express에서 HTML을 뷰 엔진으로 사용하려면 어떻게합니까?
나는 씨앗 에서이 간단한 변경을 시도하고 해당 .html 파일 (예 : index.html)을 만들었습니다.
//app.set('view engine', 'jade');
app.set('view engine', 'html');
이 파일은 동일하게 유지되었습니다.
exports.index = function(req, res){
res.render('index');
};
그러나 달리는 동안 나는 얻는다
500 오류 : 'html'모듈을 찾을 수 없습니다
'ejs'를 사용하는 유일한 옵션입니까? 내 의도는 AngularJS와 함께 일반 HTML을 사용하는 것이 었습니다.
다른 링크의 답변은 효과가 있지만 HTML을 제공하기 위해 펑키 라우팅을 설정하지 않는 한 뷰 엔진을 전혀 사용할 필요가 없습니다. 대신 정적 미들웨어를 사용하십시오.
app.use(express.static(__dirname + '/public'));
렌더 엔진이 jade 대신 html을 허용하도록하려면 다음 단계를 수행하십시오.
-
npm install consolidate npm install swig
app.js 파일에 다음 줄을 추가하십시오.
var cons = require('consolidate'); // view engine setup app.engine('html', cons.swig) app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'html');
“views”폴더 안에 .html로 뷰 템플릿을 추가하십시오. 노드 서버를 다시 시작하고 브라우저에서 앱을 시작하십시오.
이것이 아무런 문제없이 HTML을 렌더링하지만 JADE를 배우는 것이 좋습니다. Jade는 놀라운 템플릿 엔진이며이를 배우면 더 나은 디자인 및 확장 성을 달성하는 데 도움이됩니다.
apps.js에 추가하십시오.
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
이제 뷰 파일을 .html로 유지하면서 ejs 뷰 엔진을 사용할 수 있습니다
출처 : http://www.makebetterthings.com/node-js/how-to-use-html-with-express-node-js/
이 두 패키지를 설치해야합니다.
`npm install ejs --save`
`npm install path --save`
그런 다음 필요한 패키지를 가져옵니다.
`var path = require('path');`
이렇게하면 뷰 를 .ejs 대신 .html 로 저장할 수 있습니다 . html을 지원하지만 ej를 인식하지 못하는 IDE로 작업하는 동안 매우 유용합니다.
서버 구성에 이것을 시도하십시오
app.configure(function() {
app.use(express.static(__dirname + '/public')); // set the static files location
app.use(express.logger('dev')); // log every request to the console
app.use(express.bodyParser()); // pull information from html in POST
app.use(express.methodOverride()); // simulate DELETE and PUT
app.use(express.favicon(__dirname + '/public/img/favicon.ico'));
});
then your callback functions to routes will look like:
function(req, res) {
res.sendfile('./public/index.html');
};
No view engine is necessary, if you want to use angular with simple plain html file. Here's how to do it: In your route.js
file:
router.get('/', (req, res) => {
res.sendFile('index.html', {
root: 'yourPathToIndexDirectory'
});
});
Answer is very Simple. You Must use app.engine('html') to render *.html Pages. try this.It must Solve the Problem.
app.set('views', path.join(__dirname, 'views'));
**// Set EJS View Engine**
app.set('view engine','ejs');
**// Set HTML engine**
app.engine('html', require('ejs').renderFile);
the .html file Will work
HTML files can be rendered using ejs engine:
app.set('view engine', 'ejs');
And make sure your files under "/views" are named with ".ejs".
For example "index.ejs".
I recommend using https://www.npmjs.com/package/express-es6-template-engine - extremely lightwave and blazingly fast template engine. The name is a bit misleading as it can work without expressjs too.
express-es6-template-engine
앱 에 통합 하는 데 필요한 기본 사항 은 매우 간단하고 구현하기가 매우 쉽습니다.
const express = require('express'),
es6Renderer = require('express-es6-template-engine'),
app = express();
app.engine('html', es6Renderer);
app.set('views', 'views');
app.set('view engine', 'html');
app.get('/', function(req, res) {
res.render('index', {locals: {title: 'Welcome!'}});
});
app.listen(3000);
index.html
파일 의 내용은
'views'디렉토리 안에 있습니다.
<!DOCTYPE html>
<html>
<body>
<h1>${title}</h1>
</body>
</html>
html은 뷰 엔진이 아니지만 ejs는 그 안에 html 코드를 작성할 수있는 가능성을 제공합니다
라우팅을 통해 서버 HTML 페이지에 연결했습니다.
var hbs = require('express-hbs');
app.engine('hbs', hbs.express4({
partialsDir: __dirname + '/views/partials'
}));
app.set('views', __dirname + '/views');
app.set('view engine', 'hbs');
.html 파일의 이름을 .hbs 파일로 변경했습니다.-핸들 바는 일반 HTML을 지원합니다
렌더 엔진이 jade 대신 html을 허용하도록하려면 다음 단계를 수행하십시오.
Install consolidate and swig to your directory.
npm install consolidate
npm install swig
add following lines to your app.js file
var cons = require('consolidate');
// view engine setup
app.engine('html', cons.swig)
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');
add your view templates as .html inside “views” folder. Restart you node server and start the app in the browser.
이 작동합니다
이 간단한 해결책을 시도해보십시오.
app.get('/', function(req, res){
res.render('index.html')
});
HTML 미들웨어 주석
//app.set('view engine', 'html');
대신 다음을 사용하십시오.
app.get("/",(req,res)=>{
res.sendFile("index.html");
});
참고 URL : https://stackoverflow.com/questions/17911228/how-do-i-use-html-as-the-view-engine-in-express
'programing tip' 카테고리의 다른 글
JAR 파일 외부의 특성 파일 읽기 (0) | 2020.07.14 |
---|---|
Java 용 BDD 프레임 워크의 차이점은 무엇입니까? (0) | 2020.07.14 |
JUnit 대 TestNG (0) | 2020.07.14 |
Mockito와 JMockit의 비교-Mockito가 JMockit보다 더 나은 투표를하는 이유는 무엇입니까? (0) | 2020.07.14 |
유성 테스트 주도 개발 (0) | 2020.07.14 |