Answer the question
In order to leave comments, you need to log in
What are the frontend options for node.js?
Существуют js-фреймворки (angular, backbone и т.д. ) которые общаются с сервером по средствам ajax запросов, получая от сервера json с данными, а затем выводя эти данные в браузере. При этом страница генерируется на стороне клиента непосредственно самом браузере. С этим я вроде разобралась. Но возникло несколько вопросов:
1) Всегда ли сервер ноды передает данные в формате json?
2) Может ли сервер ноды вернуть сгенерированную страницу, как например php сервер?
3) Если это возможно, то где об этом почитать и как это выглядит?
Answer the question
In order to leave comments, you need to log in
1) Does the node server always send data in json format?
Of course not.
Do you mean without AJAX at all, or with AJAX but with an HTML response?
It is possible to do both.
1) Does the node server always send data in json format?No, whatever you want, in that you will pass it on.
2) Can the node server return the generated page, like a php server for example?Maybe. html, json, yaml, xml - whatever you want, whatever you need.
3) If it is possible, where can I read about it and what does it look like?In short, it usually looks something like this:
res.render('path/to/view', someData)
or res.json(someData)
(when using express, which, by the way, is completely optional). You can read in any entry-level tutorial on any web framework (most of all materials on express )
In express, this is done like this:
var express = require('express');
var path = require('path');
var appDir = path.dirname(require.main.filename);
var router = express.Router();
router.get('/', (req, res, next) => {
var mainView = path.resolve(appDir, 'views', 'index.html');
if(mainView){
res.sendFile(mainView);
} else {
next();
}
});
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Article = mongoose.model('Article');
module.exports = function (app) {
app.use('/', router);
};
router.get('/', function (req, res, next) {
Article.find(function (err, articles) {
if (err) return next(err);
res.render('index', {
title: 'Home page',
articles: articles
});
});
});
Read, watch here: Ilya Kantor: Node.js Screencast
Noda can do the same thing as a php server
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question