简体   繁体   English

在 node.js 中存储数据

[英]Storing data in node.js

I got this assessment project from a company which involves HTML,CSS and JavaScript.我从一家涉及 HTML、CSS 和 JavaScript 的公司获得了这个评估项目。 They sent me a zip file and I'm supposed to implement the app (which is an address book) using the node.js they sent.他们给我发送了一个 zip 文件,我应该使用他们发送的 node.js 来实现应用程序(它是一个地址簿)。

The problem is I don't know much about node.js.问题是我对 node.js 了解不多。 I've been reading a bunch of stuff online but I still couldn't figure out how to store data.我一直在网上阅读了很多东西,但我仍然无法弄清楚如何存储数据。 The instructions said that the data is already managed by the server and can be interacted using this API:说明说数据已经由服务器管理,可以使用这个API进行交互:

API:应用程序接口:

GET - api/contacts/
params: none
returns list of contacts


GET - api/contacts/:id
params: {
    id: int
}
returns contact with given id or null if contact does not exist


POST - api/contacts/
data: {
    firstName: string,
    lastName: string,
    email: string
} 
returns saved contact


PUT - api/contacts/
data: {
    id: int,
    contact: 
    {
        firstName: string,
        lastName: string,
        email: string
    }
}
return updated contact


DELETE - api/contacts/:id
params: {
    id: int
}
returns a true or false depending on if contact was successfully deleted

I don't know how to approach this problem.我不知道如何处理这个问题。 This is for a front-end internship position.这是一个前端实习职位。 I thought about using ajax or something but I think I may be just making it more complicated than it has to be because that's backend stuff.我想过使用 ajax 或其他东西,但我认为我可能只是让它变得比它更复杂,因为那是后端的东西。 There has to be an easier way of doing this like maybe use a function/method or something?必须有一种更简单的方法来做到这一点,比如使用函数/方法之类的?

Update更新

I spent all Monday night brushing up on JavaScript (I'm a senior at university that doesn't teach JS so everything is self-taught) then I even studied backbone.js that same night.我整个星期一晚上都在复习 JavaScript(我是大学四年级学生,不教 JS,所以一切都是自学的),然后我什至在同一天晚上学习了 Backbone.js。 I thought it was a bit complicated so I decided to not do it that way because I don't want the company to think I know backbone.js.我觉得这有点复杂,所以我决定不这样做,因为我不想让公司认为我知道骨干.js。

I didn't even know what to do in the beginning because I didn't know node.js either so I also had to do a lot of reading about that.一开始我什至不知道该做什么,因为我也不知道 node.js,所以我也不得不阅读大量相关内容。 I was thinking of several ways of approaching it and just really needed some guide but couldn't find any.我正在考虑几种接近它的方法,只是真的需要一些指导,但找不到任何指导。

Anyway thank you very much for the example.无论如何,非常感谢你的例子。 I learn best by seeing examples.我通过看例子学习得最好。

It looks like a restful API from the nodejs file sent to you, so using AJAX requests would be the right thing to do.它看起来像是发送给您的 nodejs 文件中的一个宁静的 API,因此使用AJAX 请求将是正确的做法。

For example:例如:

$.ajax({
  type: 'POST',
  url: 'http://localhost:nodeport/api/contacts/'
  data: { firstName: 'John', lastName: 'Smith', email: 'j.smith@example.com' }
  success: function(data, status, xhr) { window.alert('Contact '+data.firstName+' saved'); }
});

would store the data in the server and return the saved contact in the success callback.将数据存储在服务器中并在成功回调中返回保存的联系人。

Well, first you need to use Express framework where you can easely setup routes for your API.好吧,首先您需要使用Express框架,您可以在其中轻松地为您的 API 设置路由。 For data storage, use MongoDB and Mongoose , which is kind of abstraction for mongodb with some helpful stuff out of the box.对于数据存储,使用MongoDBMongoose ,这是对 mongodb 的一种抽象,带有一些开箱即用的有用东西。

That's basically all.这基本上就是全部。

I think the backend data is handled by the node.js, leave it.我认为后端数据由 node.js 处理,离开它。 Knowing the REST api you should know with GET you get some data, with POST you create and store data, with PUT you update data and with DELETE you delete data.了解REST api,您应该知道使用GET获取一些数据,使用POST创建和存储数据,使用PUT更新数据,使用DELETE删除数据。 Each call will give you an answer so you know your ajax call was handled well by the backend.每个调用都会给你一个答案,所以你知道你的 ajax 调用被后端处理得很好。 As a starter you are, I suggest you use jQuery ajax calls and try to get it done.作为初学者,我建议您使用 jQuery ajax 调用并尝试完成它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM