i am new in node.js and express.
我是node.js的新手并表达。
in my test app the html variables will not be parsed. what is the problem?
在我的测试应用程序中,不会解析html变量。问题是什么?
server.js
server.js
enter var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({
extended : true
}));
app.use(express.static(__dirname));
app.get('/', function(req, res) {
res.render('index', {
wert : 'hallo'
});
});
app.post('/', function(req, res) {
var wert = req.body.wert;
var html = 'Hallo: '+wert+'!\n'
+ '<a href="/">Mach\'s nochmal!</a>';
res.send(html);
});
app.listen(8882);
console.log('Server port: 8882');
index.html:
index.html的:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Startseite</title>
</head>
<body>
<p>Hallo <%= wert %>, dies ist mein kleiner Webserver!</p>
<br>
<form action="/" method="post">
<input type="text" width="40" name="wert" value="<%= wert %>">
<input type="submit" value="submit">
</form>
</body>
</html>
the value parameter <%= wert %> in the view is not parsed. why? how to do it?
不解析视图中的值参数<%= wert%>。为什么?怎么做?
thank you jet
谢谢你
2 个解决方案
#1
3
Express doesn't come with a view-engine by default. You need to add one, in your case EJS.
Express默认情况下不带有视图引擎。您需要添加一个,在您的情况下EJS。
Using the command line, type:
使用命令行键入:
npm install ejs
server.js now becomes:
server.js现在变为:
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.engine('.html', require('ejs').__express);
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
app.use(bodyParser.urlencoded({
extended : true
}));
app.use(express.static(__dirname));
app.get('/', function(req, res) {
res.render('index', {
wert: 'hallo'
});
});
app.post('/', function(req, res) {
var wert = req.body.wert;
res.render('index', {
wert: wert
});
});
app.listen(8882);
#2
0
thank you ben,
谢谢你,
your ejs
configuration was good. I had to install express
, ejs
, body-parser
. that was ok. then i had to remove the line with app.use(express.static(__dirname));
你的ejs配置很好。我不得不安装express,ejs,body-parser。那没关系。然后我不得不用app.use删除该行(express.static(__ dirname));
and i had to change
我不得不改变
app.set('views', __dirname + '/views');
to
app.set('views', __dirname);
for my original path.
为了我原来的道路。
then everything was ok!
一切都很好!
greez jet
格力喷气机
#1
3
Express doesn't come with a view-engine by default. You need to add one, in your case EJS.
Express默认情况下不带有视图引擎。您需要添加一个,在您的情况下EJS。
Using the command line, type:
使用命令行键入:
npm install ejs
server.js now becomes:
server.js现在变为:
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.engine('.html', require('ejs').__express);
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
app.use(bodyParser.urlencoded({
extended : true
}));
app.use(express.static(__dirname));
app.get('/', function(req, res) {
res.render('index', {
wert: 'hallo'
});
});
app.post('/', function(req, res) {
var wert = req.body.wert;
res.render('index', {
wert: wert
});
});
app.listen(8882);
#2
0
thank you ben,
谢谢你,
your ejs
configuration was good. I had to install express
, ejs
, body-parser
. that was ok. then i had to remove the line with app.use(express.static(__dirname));
你的ejs配置很好。我不得不安装express,ejs,body-parser。那没关系。然后我不得不用app.use删除该行(express.static(__ dirname));
and i had to change
我不得不改变
app.set('views', __dirname + '/views');
to
app.set('views', __dirname);
for my original path.
为了我原来的道路。
then everything was ok!
一切都很好!
greez jet
格力喷气机