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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

How to create a self hosted API for development and testing

27 点作者 objnotdefined大约 11 年前

7 条评论

couchand大约 11 年前
As long as you&#x27;re just working on JavaScript code for the front end, wouldn&#x27;t it make sense to write this in node? That way you can avoid context switching. I&#x27;d daresay this would be much simpler in JavaScript as well, to the point that a tutorial isn&#x27;t really necessary.<p>Here&#x27;s a quick take. I&#x27;m sure it can be cleaned up further. I&#x27;ve also taken the liberty of fixing the delete before post id bug.<p><pre><code> express = require &#x27;express&#x27; bodyParser = require &#x27;body-parser&#x27; server = express() server.use bodyParser() books = [ { id: 1, title: &quot;Microsoft Visual C# 2012&quot;, author: &quot;John Sharp&quot; } { id: 2, title: &quot;C# 5.0 in a nutshell&quot;, author: &quot;Joseph Albahari&quot; } { id: 3, title: &quot;C# in Depth, 3rd Edition&quot;, author: &quot;Jon Skeet&quot; } { id: 4, title: &quot;Pro ASP.NET MVC 5&quot;, author: &quot;Adam Freeman&quot; } ] findBook = (req, res, cb) -&gt; book = books.filter (b) -&gt; b.id is +req.params.id if book.length cb book[0] else res.statusCode = 404 res.send &quot;No book with ID = #{req.params.id}&quot; server.get &quot;&#x2F;api&#x2F;books&quot;, (req, res) -&gt; res.send books server.get &quot;&#x2F;api&#x2F;books&#x2F;:id&quot;, (req, res) -&gt; findBook req, res, (book) -&gt; res.send book server.post &quot;&#x2F;api&#x2F;books&quot;, (req, res) -&gt; book = req.body book.id = 1 + Math.max.apply Math, books.map (b) -&gt; b.id books = books.concat [book] res.send 200 server.put &quot;&#x2F;api&#x2F;books&#x2F;:id&quot;, (req, res) -&gt; findBook req, res, (book) -&gt; updates = req.body [&#x27;title&#x27;, &#x27;author&#x27;].forEach (f) -&gt; book[f] = updates[f] res.send 200 server.delete &quot;&#x2F;api&#x2F;books&#x2F;:id&quot;, (req, res) -&gt; findBook req, res, (book) -&gt; books = books.filter (b) -&gt; b.id isnt book.id res.send 200 server.listen 3000 console.log &quot;server listening on localhost:3000&quot;</code></pre>
Xdes大约 11 年前
You can allow non-administrator access using netsh[1] as an administrator. That way you don&#x27;t need to run VS as admin.<p><pre><code> netsh http add urlacl url=http:&#x2F;&#x2F;+:8080&#x2F; user=DOMAIN\username </code></pre> You don&#x27;t need attributes based routing in this example. Convention based routing is built into WebAPI.<p>For the Delete method you can return an HttpStatusCode instead of throwing an exception.<p><pre><code> public IHttpActionResult Delete(int Id) { var result = (from b in ourbooks where b.Id == Id select b).FirstOrDefault(); ourbooks.Remove(result); return StatusCode(HttpStatusCode.Accepted); } </code></pre> Also check out the OWIN self host tutorial [2].<p>[1] <a href="https://github.com/NancyFx/Nancy/wiki/Hosting-nancy-with-owin" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;NancyFx&#x2F;Nancy&#x2F;wiki&#x2F;Hosting-nancy-with-owi...</a><p>[2] <a href="http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api" rel="nofollow">http:&#x2F;&#x2F;www.asp.net&#x2F;web-api&#x2F;overview&#x2F;hosting-aspnet-web-api&#x2F;u...</a>
revetkn大约 11 年前
Is the normal way to returning &quot;success&quot; values (throwing an exception?)<p><pre><code> var resp = new HttpResponseMessage(HttpStatusCode.Created); throw new HttpResponseException(resp);</code></pre>
fredkelly大约 11 年前
Apiary is also a good option ( <a href="http://apiary.io" rel="nofollow">http:&#x2F;&#x2F;apiary.io</a>).
评论 #7743902 未加载
JamesBaxter大约 11 年前
Does anyone know of a similar guide for Ruby? I&#x27;m an ASP.net developer and I&#x27;m trying to get some insight into other (perhaps quicker&#x2F;easier) ways of doing exactly what this guide describes.
评论 #7743220 未加载
calgaryeng大约 11 年前
<a href="https://github.com/typicode/json-server" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;typicode&#x2F;json-server</a>
myhf大约 11 年前
That&#x27;s the first time I&#x27;ve heard that meaning of &quot;self hosted.&quot; I was expecting something like a REST server that also acted as a client to another instance of itself.