在为服务器提供API的同时可以提供静态文件吗? [英] Can I serve static files while providing an API for the server?

查看:91
本文介绍了在为服务器提供API的同时可以提供静态文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我对Web开发还很陌生,现在我和我的更多面向Web的朋友开始了一个项目,他把各种框架都扔给了我.我们正在做Vuejs,玉器,手写笔和jeet.对于新手来说,这当然很令人困惑,因为没有Vuejs实例使用玉石,没有Jade实例使用vuejs等.

但是,对于这个项目,我们需要一个可以处理对Google地图的api调用,保存内容等的后端.我们俩都没有这样做的经验,我尝试在Rust中进行构建,并使其全部与api一起使用但是我无法提供文件,导致我们尝试使用http服务器提供文件,然后从客户端对Rust后端进行api调用.这导致了一些问题,因为我们不得不执行CORS请求(?),而我没有上班.

很抱歉,长期以来,所有问题归结为一个问题:我如何提供静态文件,同时又可以对Google Maps进行api调用并将内容存储在数据库中?我发现的所有示例似乎都假设您正在使用模板来生成提供给最终用户的文件?

我该如何解决这个问题?我的朋友已经完成了大部分前端工作,只需使用npm包"http-server"即可工作

解决方案

您可以在Node.js中使用 express框架

它可以处理api请求以及静态文件

静态文件可以使用 express.static 中间件

作为服务器

您可以将此答案作为快速参考: https://stackoverflow.com/a/36639721/3359432

阅读此链接以获取有关使用express创建api服务器的信息: http://expressjs.com/en/guide/routing.html

阅读此链接可了解有关使用express服务静态文件的更多信息: http://expressjs.com/en/starter/static-files.html

So I'm pretty new to web development and now that me and my much more web oriented friend started a project, he threw all kind of frameworks on me. We're doing Vuejs, jade, stylus, and jeet. For a newcomer, this is of course very confusing as no Vuejs examples uses jade, no jade examples uses vuejs, etc.

However, for this project we need a backend which can handle api calls to Google maps, saving stuff, etc. Neither of us have experience doing this and I tried to build it in Rust and got it all working with the api part but I couldn't manage to serve the files, leading to us trying a http-server serving the files and then making api calls to the Rust backend from the client. This led to problems as we had to do CORS requests (?) which I didn't get to work.

Sorry for the long background, it all boils down to the question: How do I serve static files while having the possibility to make api calls to Google Maps and store stuff in a database? All examples I find seems to assume that you're using templates to generate the files served to the end user?

How do I attack this problem? My friend has finished most of the frontend and it works simply by using the npm package "http-server"

解决方案

You can use express framework in Node.js

It can server api requests as well as static files

Static files can be server using express.static middleware

You can refer this answer for a quick reference: https://stackoverflow.com/a/36639721/3359432

Read this link for info on creating api server using express: http://expressjs.com/en/guide/routing.html

Read this link to know more about serving static files using express: http://expressjs.com/en/starter/static-files.html

这篇关于在为服务器提供API的同时可以提供静态文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆