2016-10-01 24 views
2

試圖呈現從‘若干意見’,從‘路線’目錄下的文件,但得到以下錯誤:錯誤:ENOENT:沒有這樣的文件或目錄,打開」 ./views/s.ejs'快速的NodeJS

Error:
valid Login
{ [Error: ENOENT: no such file or directory, open './views/s.ejs']
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: './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

我將ejs.renderFile更改爲res.render並且工作正常。

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

你並不需要指定view的路徑,快遞模板提供這方面的知識:

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

如果在視圖中有用戶目錄,並且在profile.ejs中,則只需res.render('user/profile.ejs'),依此類推。

+0

已經嘗試過:DB結果:[對象的對象] 有效登錄 {[錯誤:ENOENT:沒有這樣的文件或目錄中,打開 's.ejs']錯誤號:-2,代碼: 'ENOENT' ,syscall:'open',路徑:'s.ejs'} [0mPOST/afterSignIn [32m200 [0m104.060 ms - - [0m – ikck

相關問題