TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: How to build static website that displays database data

14 点作者 lowpro超过 4 年前
I&#x27;ve been trying to figure out how to build a &quot;static&quot; website that displays data pulled from a database. I know SQL fairly well, but have no experience in web dev. I would think website builders would have basic functionality for this, but maybe it&#x27;s a much harder problem than I&#x27;m thinking.<p>I would also think this could be done on the server side, so the user should never interact directly with the database. Is web development usually difficult to find answers for? I work in networking so rarely interactive with web dev.<p>Thank you.

13 条评论

lovelearning超过 4 年前
See <a href="https:&#x2F;&#x2F;gohugo.io&#x2F;templates&#x2F;data-templates&#x2F;" rel="nofollow">https:&#x2F;&#x2F;gohugo.io&#x2F;templates&#x2F;data-templates&#x2F;</a>. If data is SQL, a simple API server to serve up the data at generation-time is the preferred approach.<p>Pelican and Hugo are the two generators I&#x27;m comfortable. Both have a plugin ecosystem - my guess is that something close already exists.<p>Otherwise, Pelican source code is simple to understand and extend. If I were to implement this and if the data was static or changed only occasionally, I&#x27;d read my custom data source, add that data to the Jinja template variables and access them from Jinja HTML templates. For fast changing data, I agree with the export to JSON and render from Javascript approach already suggested.
mattmanser超过 4 年前
Do you know the meaning of &#x27;static website&#x27;? In some ways your question is an oxymoron. You&#x27;re misunderstanding the term and have asked a bit of a silly question by accident.<p>If you explained why you think it needs to be static, we might be able to explain what you&#x27;ve misunderstood.<p>Ultimately some part of your system will need to be dynamic to get the data and transmit it across the internet. A static website can technically use JavaScript to call a dynamic one using an API, and then display data, but part of the overall setup is still dynamic. A normal website is still involved, that has to talk to the db. The setup is just more complicated.<p>As for the actual question, pick any framework (Rails, Django, etc). Follow the tutorial for beginners, they cover how to access the db within an hour or so.
评论 #24696182 未加载
评论 #24691000 未加载
majkinetor超过 4 年前
So far other answers are limited in what they consider a &#x27;database&#x27;. If you need full blown relational db support, you could for example utilize PostgreRest to get db API with minimal effort that you can use as a service. This is not generation time (it could be), the point is to work run time. This is similar to how you would handle comments via for example discuss service. You basically have full SQL capabilities via REST interface - you can choose, order, sort and filter any db column, 100% of CRUD and even have actions via stored procedures. After taking a time to learn it, you can create a production grade service in few hours.<p>Data onboarding is as trivial as creating a table in a database - it will get automagically exposed as REST endpoint.<p>Performance is epic (my tests show ~2K req&#x2F;s).<p>There are alternative services, such as hasura, you might wanna take a look at.<p><a href="http:&#x2F;&#x2F;postgrest.org&#x2F;en&#x2F;v7.0.0&#x2F;" rel="nofollow">http:&#x2F;&#x2F;postgrest.org&#x2F;en&#x2F;v7.0.0&#x2F;</a><p>Here is my Windows setup&#x2F;test and in the notes section you have another one for Nix.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;majkinetor&#x2F;postgrest-test" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;majkinetor&#x2F;postgrest-test</a>
cpach超过 4 年前
Here’s an idea: Generate the data and publish it as JSON file. Then you can use some JavaScript to load and display the data. I think Datatables can do this.<p><a href="https:&#x2F;&#x2F;datatables.net&#x2F;" rel="nofollow">https:&#x2F;&#x2F;datatables.net&#x2F;</a>
undecisive超过 4 年前
I think the correct answer to this is:<p>- Create an API that exposes the data from the data you need<p>- Consume that API from your static site.<p>As others have mentioned, there are tools out there that help you in implementing the API fairly simply. The reason you shouldn&#x27;t just connect to the database from the browser is primarily security - you should build your API to be a gatekeeper, and expose only the data you need to expose.<p>How you consume that is up to you. You may find you have some issues with things like CORS, depending on how you set this up - but these issues can usually be worked around.<p>[edit: Note that depending on why you want a static site, e.g. if bandwidth costs are your big limit and you don&#x27;t want your API hit with every request, you might find that setting up an automated worker to scrape your data and deploy it makes life easier - assuming that all your visitors to a page will want access to exactly the same data. In fact, if you save it as JSON with a filename ending .js, prepend &quot;window.page_content = &quot; to your file as part of your worker, then you can load your database content at the top of your page in a simple script tag and use that data in the rest of your page.]
chmaynard超过 4 年前
Static site generators can generally load serialized data in formats such as CSV, JSON, or YAML when the site is generated. If you can export data from your database in one of those formats, then you&#x27;re good to go. The documentation for the generator you&#x27;re using explains how to make use of this data in your markdown or HTML source files.
laurieg超过 4 年前
Before I say this, I have to say: I am not making fun of you or insulting you at all.<p>However, your question is like asking &quot;How do I make a horse that is actually a car?&quot;<p>The two things are just completely different. Static websites, by definition, don&#x27;t have databases and constantly changing information.<p>So, it sounds like you want to learn some web development. I recommend trying The Rails Tutorial.[1] It teaches your everything you need to know to start making web applications and doesn&#x27;t assume you have lots of previous experience.<p>Good luck! And when you make something cool come back and post a &quot;Show HN&quot;.<p>[1]<a href="https:&#x2F;&#x2F;www.railstutorial.org&#x2F;book" rel="nofollow">https:&#x2F;&#x2F;www.railstutorial.org&#x2F;book</a>
yardshop超过 4 年前
I found this library a couple years ago when making simple HTML reports from Powershell scripts, to be viewed locally from a network share:<p><pre><code> AlaSQL JavaScript SQL Database Library http:&#x2F;&#x2F;alasql.org&#x2F; </code></pre> Works really nicely to show your data in a table and allow sorting by columns and running predefined or ad hoc queries. It&#x27;s a JavaScript SQL database, so you need to get your data into it primarily as JSON, but it supports importing from text, CSV, and some other common formats, and exporting too.
solus_factor超过 4 年前
Usually this is done server-side in something like PHP. It will not be a &quot;static&quot; website, though. The script will generate an HTML page on each access and return it to the user.<p>Alternatively, if DB data rarely changes, you can generate and save static HTML&#x27;s on the server and then serve them. But usually if people are accessing database, they work with dynamic data.
nabla9超过 4 年前
Both sqlite and postgresql have options where you can change query result format into html. I think many others have too.
评论 #24685567 未加载
shyn3超过 4 年前
You can dump your tables to a .html file on a schedule and then serve it via a CloudFlare cache with an expiry so that your reports are generated once every 5 minutes and the first request is served by your server then it&#x27;s served via the cache until you refresh it.
shoo超过 4 年前
One strange direction to explore could be to set up a webserver to serve a directory full of files, rig your db to look like a filesystem, then compose the two.<p>This is a probably a daft idea but searching &quot;fuse postgresql&quot; produces results, so it should be an easy daft idea!
shanecleveland超过 4 年前
Is the data &quot;static?&quot; Perhaps something like <a href="https:&#x2F;&#x2F;www.sheet2site.com" rel="nofollow">https:&#x2F;&#x2F;www.sheet2site.com</a> is worth looking at.