错误:ENOENT:没有这样的文件或目录,打开'./views/s.ejs'Nodejs Express

时间:2016-10-01 23:02:25

标签: javascript html mysql node.js express

尝试从'routes'目录中的'views'目录中渲染文件,但是会出现以下错误:

  

错误:   
有效登录   
{[错误:ENOENT:没有这样的文件或目录,打开'./views/s.ejs']   
errno:-2,   
代码:'ENOENT',   
系统调用:'打开',   
路径:'。/ views / s.ejs'}   
[0mPOST / afterSignIn [32m200 [0m77.339 ms - - [0m

afterSiginIn.js(Routes Directory)

var ejs = require("ejs");
var mysql = require('./mysql');
var express = require('express');
var router = express.Router();
router.post('/',function(req,res,next)
{enter code here
console.log("reached after sign in checking if user exists");
// check user already exists
var getUser="select * from users where emailid='"+req.param("inputUsername")+"' and password='" + req.param("inputPassword") +"'";
    console.log("Query is:"+getUser);

    mysql.fetchData(function(err,results){
        if(err){
            throw err;
        }
        else 
        {
            if(results.length > 0){
                console.log("valid Login");
                ejs.renderFile('./views/s.ejs', { data: results } , function(err, result) {
                    // render on success
                    if (!err) {
                        res.end(result);
                    }
                    // render or error
                    else {
                        res.end('An error occurred');
                        console.log(err);



app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');
var signin = require('./routes/signin');
var register = require('./routes/register');
var afterSignIn=require('./routes/afterSignIn');

var app = express();


app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
app.use('/signin', signin);
app.use('/register', register);
app.use('/afterSignIn', afterSignIn);

s.ejs
<!DOCTYPE html>
<html>
  <head>
    <title>Successful Login</title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
  </head>
  <body>
    <h1>All Users in Database</h1>
    <div>
           <% for(var i in data) 
                {                     
            %>
                    <p><strong>EmailId: </strong><%= data[i].username %></p>
                    <p><strong>Password: </strong><%= data[i].password %></p>
                    <br>       
           <% 
                }  
           %>
    </div>
  </body>
</html>

directory structure:

2 个答案:

答案 0 :(得分:2)

我将ejs.renderFile更改为res.render并且有效。

res.render('s.ejs', { data: results } , function(err, result)

答案 1 :(得分:1)

您不需要指定view的路径,快递模板提供以下知识:

res.render('s.ejs', {data: 'some data'});

如果在视图中有用户目录,并且在profile.ejs内,则只需res.render('user/profile.ejs'),依此类推。