简体   繁体   English

使用Node.js + Express构建一个简单的SPA

[英]Build a simple SPA using Node.js + Express

I develop a web application using Node.js for the Back End and HTML/CSS/JS as Front End. 我使用后端的Node.js和前端的HTML / CSS / JS开发了一个Web应用程序。 I am attempting to build a single page app using Express framework. 我正在尝试使用Express框架构建单页应用程序。

I want to build a single page application using just Node.js or JavaScript, I know that for the response we could call something like res.render('contact') , but this is not working in my case because I wan't to change dynamically my page, I don't want any additional frameworks like React, Angular, ... 我想仅使用Node.js或JavaScript构建一个单页应用程序,我知道对于响应我们可以调用诸如res.render('contact') ,但这在我的情况下不起作用,因为我不想动态更改我的页面,我不需要任何其他框架,例如React,Angular,...

On my index.html page, there is a menu that each of its elements contain a hyperLink. 在我的index.html页面上,有一个菜单,其每个元素都包含一个hyperLink。 For example when clicking on contact hyperLink in index page, I want to display on the same page dynamically the content of contact page with forms...ect without loading the page : 例如,当单击索引页面中的联系人超级链接时,我想在同一个页面上动态显示具有表单... ect的联系人页面的内容,而无需加载页面:

    <li> <a href="/contact">Contact</a></li>

My server.js file that defines the routes, by default I am directed to index page : 我的server.js文件定义了路由,默认情况下,我被定向到index页面:

//to define the routes
var express = require('express'), app = express();
//to call multiple template engines easly, like html page...ect
var engines = require('consolidate');

app.engine('html', engines.nunjucks);
app.set('view engine', 'html');
app.set('views', __dirname + '/public');
app.use(express.static(__dirname + '/public'));


app.get('/', function(req, res){
    res.render('index');
});

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


var server = app.listen(8080, function(){
    console.log('Listening on port ' + server.address().port);
});

I know that SPA live on a single HTML document, but how I can do this with Express Framework without loading the pages? 我知道SPA位于单个HTML文档中,但是如何使用Express Framework做到这一点而不加载页面? I am a bit lost. 我有点迷路。

Your question is very broad, but I'll try to explain a few things that may help. 您的问题非常广泛,但是我将尝试解释一些可能有所帮助的事情。

The job of Express is to handle HTTP requests and return responses. Express的工作是处理HTTP请求并返回响应。 Some of those responses will be HTML files, perhaps with JavaScript and CSS. 其中一些响应将是HTML文件,可能带有JavaScript和CSS。

To create an SPA, you will need to, at minimum, serve up a bundle of HTML and JavaScript to the client's browser as a starting point, usually an index.html page. 要创建SPA,您至少需要为客户端的浏览器提供一捆HTML和JavaScript作为起点,通常是一个index.html页面。

This page, through JavaScript and HTML, can listen for events, including user actions and inputs, and make calls back to other endpoints on your Express server than can return data, snippets of HTML, or whatever you need to allow you to alter the single page that you have served to the user. 此网页,通过JavaScript和HTML,可以侦听事件,包括用户操作和输入,并拨打电话回其他端点您的Express服务器上不是可以返回数据,HTML的片段,或任何你需要让你改变单一您所提供给用户的页面 But the changes must be executed by JavaScript that exists on the initial page, or scripts that are added by that initial JavaScript and executed. 但是更改必须由初始页面上存在的JavaScript执行,或者由该初始JavaScript添加并执行的脚本执行。

You could for example use jQuery or fetch to make calls back to your server, or even the basic web APIs in the browser. 例如,您可以使用jQuery或fetch来调用服务器,甚至浏览器中的基本Web API There are other tools available as well. 还有其他可用工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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