nodejs中的ejs视图引擎未显示utf-8字符集

时间:2019-12-14 08:09:53

标签: node.js ejs

我得到“?”使用ejs模板引擎渲染vi​​ew.ejs时出现字符错误。 这是nodejs中的源代码:

const express = require('express');
const path = require('path');
//initial app
const app = express();
app.use(express.static(path.join(__dirname,'/public')));
//Load view engine
app.set('views',path.join(__dirname,'views'));
app.set('view engine', 'ejs');
app.get('/', function(req,res){    
res.render('index'); });
app.listen(3000, function(){
console.log("server started on port 3000...");});

例如“LêBảoCường”将变为L? B?o C ?? ng。 这是index.ejs的内容

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Application</title> 
    <link href="./css/Common.css" rel="stylesheet" />
    <link href="./css/Layout.css" rel="stylesheet" />
    <link href="./css/GridTable.css" rel="stylesheet" />
    <link href="./css/Index.css" rel="stylesheet" />
</head>
<body>
    <div class="layout-content">
        
        <div id="right-content">
            <div id="header">
                <div class="colapse-icon"></div>
            </div>
    <div id="profile-container">
    <img src="./Images/img_avatar.png" />
    <div class="content">
        <div class="line-info-wrapper">
            <div class="catefory-info">Mã nhân viên:</div>
            <div class="value-info">642</div>
        </div>
        <div class="line-info-wrapper">
            <div class="catefory-info">Tên nhân viên:</div>
            <div class="value-info">Lê Bảo Cường</div>
        </div>
        <div class="line-info-wrapper">
            <div class="catefory-info">Đơn vị:</div>
            <div class="value-info">Business Unit 1</div>
        </div>
        <div class="line-info-wrapper">
            <div class="catefory-info">Chức danh:</div>
            <div class="value-info">Lập trình viên</div>
        </div>
        <div class="line-info-wrapper">
            <div class="catefory-info">Ngày sinh:</div>
            <div class="value-info">11/01/1996</div>
        </div>
        <div class="line-info-wrapper">
            <div class="catefory-info">Tuổi:</div>
            <div class="value-info">20</div>
        </div>
    </div>
    <div id="real-content">       
        <div class="line-info-wrapper">
            <div class="catefory-info">Số lần checkin trong ngày:</div>
            <div class="value-info">20</div>
        </div>       
    </div>
</div>
<div class="container-table">
    <div class="header-table">
        <h3>Lịch sử CheckIn</h3>
    </div>
    <div class="table-wrapper">

        <table name="listProduct" gridDataUrl="/Product/GetPaging" pageSize="30" pageNumber="1" filterHeader="false">
            <thead>
                <tr>
                    <th tableIndex="ProductID" visibility="false">Mã nhân viên</th>
                    <th tableIndex="Name" style="min-width:200px;">Tên nhân viên</th>
                    <th tableIndex="ModifiedDate" style="min-width:200px;" DataType="Date">Thời gian CheckIn</th>
                    <th tableIndex="ProductNumber" style="min-width:200px;">Đơn vị</th>
                    <th tableIndex="Color" style="min-width:200px;">Chức danh</th>
                    <th tableIndex="StandardCost" style="min-width:200px;">Ngày sinh</th>
                    <th tableIndex="ListPrice" style="min-width:200px;">Tuổi</th>
                </tr>

            </thead>
            <tbody></tbody>
        </table>
        <div class="menu-context-table non-display">
            <div class="mn-wrapper" command="add">
                <div class="icon"></div>
                <div class="description">Thêm</div>
            </div>
            <div class="mn-wrapper" command="edit">
                <div class="icon"></div>
                <div class="description">Sửa</div>
            </div>
        </div>
    </div>

</div>
    </div>
        <br style="clear:both;"> <br/>
    </div>
    <script src="./js/_Layout.js"></script>   
    <script src="./js/Common.js"></script>   
    <script src="./js/GridTable.js"></script>
    <script src="./js/Index.js"></script>   
</body>
</html>
我尝试了一些方法,例如添加meta charset utf-8,但是它不起作用。 我尝试了一些方法,例如添加meta charset utf-8,但是它不起作用。 我该如何解决?

1 个答案:

答案 0 :(得分:0)

我错误地将我的index.ejs文件保存为shift_jis编码。使用utf8编码恢复该文件可以解决我的问题。