简体   繁体   中英

Node express requests

im new to node and after a few weeks practicing it, i found the express framework and i started to used it because it handle the tools i need more easy, but theres something i dont get to understand, its quite different from how do make the app without express. i dont quite get the express api well (its a bit confusing). im trying to make the request url to be found in specific url (./views). so when logi is requested, then it will do (./views/login.html)when url is(hostname/login), and so on if it contains folder. this is my code

/*
Chat application for @node.js
express version.
*/

//Load modules.
var express = require('express'),
    socket = require('socket.io'),
    swig = require('swig'),
    fs = require('fs');

//Load config.
console.log('Loading configuration.');
var config = fs.readFileSync('config.json');
var config = JSON.parse(config);
var port = config.port;
var views = config.views;
console.log('Configuration loaded.');

//Initiate express module in app.
var app = express();

// app.get('/', function(request, response)
// {
//  fs.readFile('./views/index.html', function(error, data)
//  {
//      if(error)
//      {
//          response.send('View cannot be rendered.');
//      }

//      response.type('html');
//      response.send(data);
//  });
// });

var test = "Hello";

app.engine('html', swig.renderFile);

app.set('view engine', 'html');
app.set('views', __dirname + '/views');

swig.setDefaults(
{
    cache: false
});

app.get('/', function(request, response)
{
    response.render('index', 
    {
        //Var to be named in the render : value;
        'test': test,
        'Title': 'Testing page',
    });
});

//logger.
app.use(function(request, response, next)
{
    console.log('%s %s', request.method, request.url);
    next();
});

//Set directory for static files (css, js, img);
app.use(express.static(__dirname + '/public'));

//Run the app.
app.listen(port);

im using swig module for dynamic data in html, im also comenting my tests, used app.use() for static files in ./public folder (as found in the api). so what i want to achieve is, if the url file exist, then render it with its static files(css, js). if not, return a custom html file.. im using app.get() to recieve the expecific request.(which totally i dont get).

PD: PLEASE, NEED EXPRESS TUTORIALS (better than express homepage itself.), atleast for newbies.

Since views is not in the public directory, any url with views in it will not go to the app.use() function anyway (because it can't find it there). This is good. What you need to do now is create a routing function for that specific view. Something like this above your app.use():

app.get('/views/login', function(req, res){
   res.render(__dirname + 'views/login');
});

usually, rendering engines will allow you to do a shortcut though, and just do res.render('login'); and it will find it in views by itself.

Also, some renderers allow you to specify a directory to look for when requests come in. I don't know if swig offers this though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM