如何在 Node.js 上建立网站?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8695471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:57:29  来源:igfitidea点击:

How to build a website on Node.js?

node.jshosting

提问by drelkata

I've just started diving into Node.jsafter several years of PHPprogramming and I would like to build a Node.jsapp for my next project, but I have no clue how to do it.

经过几年的PHP编程,我刚刚开始深入研究Node.js,我想为我的下一个项目构建一个Node.js应用程序,但我不知道如何去做。

Are there any resources for a Node.js jumpstart?

是否有任何有关 Node.js 快速入门的资源?

Thanks in advance!

提前致谢!

回答by alessioalex

You should think of Node.js as some kind of Apache + PHP, meaning that you can program your website and your webserver with Node.

您应该将 Node.js 视为某种 Apache + PHP,这意味着您可以使用 Node 对您的网站和 Web 服务器进行编程。

Node has some important differences with your basic PHP, it's evented, asynchronous, non-blocking. You have to learn how to deal with callbacks, don't block the event loop and other things.

Node 与您的基本 PHP 有一些重要的区别,它是事件化的、异步的、非阻塞的。你必须学习如何处理回调,不要阻塞事件循环和其他东西。

What you should do is try to learn the basic stuff with Node at the beginning, here are some great resources: https://stackoverflow.com/tags/node.js/info(my favorite has been nodetuts.comand the excellent book by it's author, Hands on Node).

你应该做的是尝试在开始时学习 Node 的基本知识,这里有一些很棒的资源:https: //stackoverflow.com/tags/node.js/info(我最喜欢的是nodetuts.com和这本优秀的书由其作者 Hands on Node)。

After you've learned the basics, you can find Expressreally useful as a web framework and Socket.IOif your app is focused on real-time.

在您学习了基础知识后,如果您的应用专注于实时,您会发现Express作为 Web 框架和Socket.IO非常有用。

回答by saintedlama

I think you're searching for a node.js jump start to build some meaningful web page with node. Take a look at express, which is a node web framework. They offer a nice but short tutorial (under guide).

我认为您正在寻找一个 node.js 跳转开始,以使用 node.js 构建一些有意义的网页。看看express,它是一个节点 Web 框架。他们提供了一个不错但简短的教程(在指南下)。

回答by benesch

You need to run Node.js on a web server. Basically, you need a VPS or Dedicated Server over which you have full control. [PHP runs through the standard web server, Apache. Node.js isthe webserver.]

您需要在 Web 服务器上运行 Node.js。基本上,您需要一个可以完全控制的 VPS 或专用服务器。[PHP 通过标准 Web 服务器 Apache 运行。Node.js网络服务器。]

Or you find a Node.js hostthat'll do it for you.

或者你会找到一个Node.js 主机来为你做这件事。

回答by Shawn Janas

Node.js is essentially your webserver that would replace Apache so the only hosting that you would find to run Nodejs is a dedicated server or a cloud instance in which you would have to install and run nodejs on. The machine that you run node.js on needs to have a domain name pointed to it or the only way you can access the server is by its IP address which is this case your localhost.

Node.js 本质上是您的网络服务器,它将取代 Apache,因此您会发现运行 Nodejs 的唯一主机是专用服务器或云实例,您必须在其中安装和运行 nodejs。你运行 node.js 的机器需要有一个指向它的域名,或者你访问服务器的唯一方法是通过它的 IP 地址,在这种情况下是你的本地主机。

回答by Jason Young

Another option is to use something like Knockout.js (http://knockoutjs.com/), and have the page make JSONP calls to the Node.js server. It's really easy to use Node to send JSON to the client, since it's JavaScript on the server. Using a framework on the client makes it really easy to create a dynamic page based on that JSON data.

另一种选择是使用类似 Knockout.js (http://knockoutjs.com/) 的东西,让页面对 Node.js 服务器进行 JSONP 调用。使用 Node 将 JSON 发送到客户端真的很容易,因为它是服务器上的 JavaScript。在客户端上使用框架可以非常轻松地基于该 JSON 数据创建动态页面。

The disadvantage is there is no graceful degredation for older browsers. The advantage is a potentially blazing fast website with great AJAX built-in right from the start.

缺点是旧浏览器没有优雅降级。优点是一个潜在的快速网站,从一开始就内置了很棒的 AJAX。

Here is some sample code for using Node to generate a JSONP response:

下面是一些使用 Node 生成 JSONP 响应的示例代码:

function writeJsonpResponse(res, jsonpcallback, obj) {
    var serialized = JSON.stringify(obj);

    res.writeHead(200, {'Content-Type': 'application/javascript'});
    res.write(jsonpcallback + '(' + serialized + ');');
    res.end();
}

回答by Justin Lu

Read the README. Setup environment. take a look at package.json (or npm init to create one) install dependencies (npm install / axios, nodemon, express, mysql, react, babel) add scripts to start server & webpack if needed Get acquainted with file structure. separation of concerns - public/dist, server, db, client think about the flow of data Ensure basic HTML structure. check that bundle.js file / everything it needs is loaded in need a div id to render react with (like app or root) Spin up Express server. Start server and webpack is separate terminals. check for console.log that server is listening! Write routes (get and post requests) on your server. check that get & post requests are working w/ Postman! Create mySQL database (db/index.js). Design & import schema (how to structure data tables). make sure you're in the db folder when importing yr schema girl! Connect db to server. check for console.log that connection is successful! Write insert/retrieve db query functions. don't forget to module.exports those queries to the server! will call query functions in routes/get & post requests in server Set up basic React structure. index.js's only task is to render your app component to the div id app need a stateful App component to render all other components remember to import & export default everything! Design the rest of your components, decide whether or not they will be functional (stateless) or class components (stateful). draw it out! what props (data & functions) do you need to pass down? Write those components. Work through one data flow that executes through all the pieces. Get your input handler to send the client side POST request (using axios) to the server route, which will execute your API call (if there is one) and then insert that data to the database. remember to handle your errors! Handle events / conditional rendering. 1. user event gives input (onChange e.target.value) 2. write a handleChange f(x) to update state to that new input 3. input is submitted (onClick) 3. write a handleSubmit f(x) that takes in the updated state and makes an axios POST request w/ that { input } to the appropriate server route remember to bind method functions appropriately! Call query functions in routes/get & post requests. Check that data from the client is being stored in the database. describe those tables my friend! Do an AJAX get request (use axios or fetch) on client side to api endpoint. Store the incoming data in setState May want to do a GET request in componentDidMount to always render appropriate info for client Yay! You successfully set up your server, database, and client, passed data between them, potentially manipulated the data, stored that data, and displayed the appropriate data to the client! Tackle those user stories!

阅读自述文件。设置环境。看看 package.json (或 npm init 来创建一个) 安装依赖项(npm install / axios、nodemon、express、mysql、react、babel) 如果需要,添加脚本以启动服务器和 webpack 熟悉文件结构。关注点分离 - public/dist, server, db, client 考虑数据流确保基本的 HTML 结构。检查 bundle.js 文件/它需要的所有内容都已加载,需要一个 div id 来呈现响应(如应用程序或根)启动 Express 服务器。启动服务器和 webpack 是独立的终端。检查服务器正在侦听的 console.log !在您的服务器上编写路由(获取和发布请求)。检查 get 和 post 请求是否与 Postman 一起工作!创建 mySQL 数据库 (db/index.js)。设计和导入模式(如何构建数据表)。确保你' 导入 yr schema girl 时重新位于 db 文件夹中!将数据库连接到服务器。检查 console.log 连接成功!编写插入/检索数据库查询函数。不要忘记 module.exports 这些查询到服务器!将在服务器中的路由/获取和发布请求中调用查询函数 设置基本的 React 结构。index.js 的唯一任务是将您的应用程序组件渲染到 div id 应用程序需要一个有状态的应用程序组件来渲染所有其他组件记得导入和导出默认所有内容!设计其余的组件,决定它们是功能性(无状态)组件还是类组件(有状态)。画出来!您需要传递哪些道具(数据和功能)?编写这些组件。处理一个通过所有部分执行的数据流。让您的输入处理程序将客户端 POST 请求(使用 axios)发送到服务器路由,这将执行您的 API 调用(如果有的话),然后将该数据插入到数据库中。记得处理你的错误!处理事件/条件渲染。1. 用户事件提供输入 (onChange e.target.value) 2. 编写一个 handleChange f(x) 以更新该新输入的状态 3. 提交输入 (onClick) 3. 编写一个 handleSubmit f(x) 接收更新的状态,并发出一个 axios POST 请求,该请求带有 { input } 到适当的服务器路由,记得适当地绑定方法函数!在路由/获取和发布请求中调用查询函数。检查来自客户端的数据是否存储在数据库中。描述那些桌子,我的朋友!在客户端向 api 端点执行 AJAX 获取请求(使用 axios 或 fetch)。将传入的数据存储在 setState 中 可能想要在 componentDidMount 中执行 GET 请求以始终为客户端呈现适当的信息 耶!您成功地设置了服务器、数据库和客户端,在它们之间传递了数据,潜在地操纵了数据,存储了该数据,并向客户端显示了适当的数据!处理那些用户故事!