json-server学习笔记二
在json-server中使用js来代替json文件。
<pre><span class="pl-c">// index.js</span> <span class="pl-c1">module</span>.<span class="pl-en">exports</span> <span class="pl-k">=</span> <span class="pl-k">function</span>() { <span class="pl-k">var</span> data <span class="pl-k">=</span> { users<span class="pl-k">:</span> [] } <span class="pl-c">// Create 1000 users</span> <span class="pl-k">for</span> (<span class="pl-k">var</span> i <span class="pl-k">=</span> <span class="pl-c1">0</span>; i <span class="pl-k"><</span> <span class="pl-c1">1000</span>; i<span class="pl-k">++</span>) { data.users.<span class="pl-c1">push</span>({ id<span class="pl-k">:</span> i, name<span class="pl-k">:</span> <span class="pl-s"><span class="pl-pds">'</span>user<span class="pl-pds">'</span></span> <span class="pl-k">+</span> i }) } <span class="pl-k">return</span> data }</pre>这个需要像nodejs一样启用该服务。
<pre>$ json-server index.js</pre>当我们访问localhost:3000/users,在bash窗口中可以看到我们能当前可访问的URL
\{^_^}/ hi! Loading index.js Done Resources :3000/users Home :3000我们访问的URL是localhost:3000/users
这里发现了,我们不需要使用document.write()这样的函数,就可以直接在浏览器窗口中输出了,在最后我们使用return。
发表评论
文章导航