L
L
Leesandra2016-05-12 13:56:51
Angular
Leesandra, 2016-05-12 13:56:51

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

4 answer(s)
R
Rou1997, 2016-05-12
@Rou1997

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.

K
Konstantin Kitmanov, 2016-05-12
@k12th

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 )

S
Super User, 2016-05-12
@sergeystepanov1988

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();
    }
});

It if to give simply html. If you need to first run through the template engine, then something like this:
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
        });
    });
});

O
OlegLustenko, 2016-05-14
@OlegLustenko

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 question

Ask a Question

731 491 924 answers to any question