[英]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. 我是Node的新手,在实践了几周后,我发现了express框架,并开始使用它,因为它可以更轻松地处理我需要的工具,但是有些我不了解,它与如何使之完全不同。没有快递的应用程序。 i dont quite get the express api well (its a bit confusing). 我不太清楚Express API(它有点令人困惑)。 im trying to make the request url to be found in specific url (./views). 即时通讯试图使请求的URL被发现在特定的URL(。/视图)。 so when logi is requested, then it will do (./views/login.html)when url is(hostname/login), and so on if it contains folder. 因此,当请求logi时,它将在URL为(hostname / login)时执行(./views/login.html),如果包含文件夹,则依此类推。 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). 我使用swig模块获取html中的动态数据,同时还对我的测试进行了整理,将app.use()用于./public文件夹中的静态文件(在api中找到)。 so what i want to achieve is, if the url file exist, then render it with its static files(css, js). 所以我要实现的是,如果url文件存在,则用其静态文件(css,js)呈现它。 if not, return a custom html file.. im using app.get() to recieve the expecific request.(which totally i dont get). 如果没有,返回一个自定义的html文件.. im使用app.get()来接收特殊请求。(我完全不知道)。
PD: PLEASE, NEED EXPRESS TUTORIALS (better than express homepage itself.), atleast for newbies. PD:请,需要快速教学(比快速首页本身更好),对于新手来说至少。
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). 由于视图不在公共目录中,因此任何包含视图的url都不会转到app.use()函数(因为在那里找不到)。 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.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'); 通常,渲染引擎将允许您执行快捷方式,而只需执行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. 另外,一些渲染器允许您指定一个目录,以在请求进入时查找。我不知道swig是否提供此功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.