Replace node JS express view engine jade with html

Keywords: Mongoose JSON

The default view engine generated by express project is jade. At that time, I preferred html pages in development, so this article introduces how to replace the default view engine jade of express with html

Step 1: open the app.js file of the project. The contents of the app.js are as follows:

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var mongoose = require('mongoose');  
var bodyParser = require('body-parser');
var ejs = require('ejs');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false}));
// view engine setup
// app.set('views', path.join(__dirname, 'views'));
// app.set('view engine', 'jade');
app.engine("html",ejs.__express);
app.set("view engine", "html");

A key:

Comment Code: app. Set ('view engine ','jade');

New code: app.set("view engine", "html");

Step 2: add a new login.html file in the views folder. The editing contents are as follows:

<h1>Login page</h1>
   <div>
        <form action="/login" method="post">
            <p>
                <label for="username">User name</label>
                <input type="text" id="username" name="username" placeholder="User name">
            </p>
            <p>
                <label for="password">Password : </label>
                <input type="text" id="password" name="password" placeholder="Password">
            </p>
            <p>
                <input type="submit" value="Sign in">
            </p>
        </form>
    </div>

 

Step 3: edit index.js under routes folder again. The editing contents are as follows:

/* GET home page. */
router.get('/', function(req, res, next) {
  res.sendfile('./views/login.html'); 
});

 

 

Posted by seanrock on Sun, 01 Dec 2019 08:23:54 -0800