Node.js Driver for SQL Server
Use Node.js to query an Azure SQL database
Painless Microsoft SQL Server with Node.js and seriate –
Delve into Code and my interest
In the past you had to configure http responses like this:
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
or
return new HttpResponseMessage(HttpStatusCode.OK);
Now you can do the following:
public IHttpActionResult GetProduct(int id) { var product = products.FirstOrDefault((p) => p.Id == id); if (product == null) { return NotFound(); } return Ok(product); }
Here are a few articles about it and a working example download
Download will work in VS 2015 or 2017 – just make sure you unlock after unzipping and use package manager to restore packages in vs.
ApiController helper methods like OK)_, NotFound() and more…
Name | Description | |
---|---|---|
![]() |
BadRequest() |
Creates a BadRequestResult. |
![]() |
BadRequest(ModelStateDictionary) |
Creates an InvalidModelStateResult with the specified model state. |
![]() |
BadRequest(String) |
Creates an T:System.Web.Http.Results.ErrorMessageResult (400 Bad Request) with the specified error message. |
![]() |
Conflict() |
Creates a ConflictResult (409 Conflict). |
![]() |
Content<T>(HttpStatusCode, T) |
Creates a <see cref=”T:System.Web.Http.NegotiatedContentResult`1″ /> with the specified values. |
![]() |
Content<T>(HttpStatusCode, T, MediaTypeFormatter) |
Creates a <see cref=”T:System.Web.Http.FormattedContentResult`1″ /> with the specified values. |
![]() |
Content<T>(HttpStatusCode, T, MediaTypeFormatter, MediaTypeHeaderValue) |
Creates a <see cref=”T:System.Web.Http.FormattedContentResult`1″ /> with the specified values. |
![]() |
Content<T>(HttpStatusCode, T, MediaTypeFormatter, String) |
Creates a <see cref=”T:System.Web.Http.FormattedContentResult`1″ /> with the specified values. |
![]() |
Created<T>(String, T) |
Creates a CreatedNegotiatedContentResult<T> (201 Created) with the specified values. |
![]() |
Created<T>(Uri, T) |
Creates a CreatedNegotiatedContentResult<T> (201 Created) with the specified values. |
![]() |
CreatedAtRoute<T>(String, IDictionary<String, Object>, T) |
Creates a CreatedAtRouteNegotiatedContentResult<T> (201 Created) with the specified values. |
![]() |
CreatedAtRoute<T>(String, Object, T) |
Creates a CreatedAtRouteNegotiatedContentResult<T> (201 Created) with the specified values. |
![]() |
Dispose() |
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
![]() |
Dispose(Boolean) |
Releases the unmanaged resources that are used by the object and, optionally, releases the managed resources. |
![]() |
Equals(Object) |
(Inherited from Object.) |
![]() |
ExecuteAsync(HttpControllerContext, CancellationToken) |
Executes asynchronously a single HTTP operation. |
![]() |
Finalize() |
(Inherited from Object.) |
![]() |
GetHashCode() |
(Inherited from Object.) |
![]() |
GetType() |
(Inherited from Object.) |
![]() |
Initialize(HttpControllerContext) |
Initializes the ApiController instance with the specified controllerContext. |
![]() |
InternalServerError() |
Creates an InternalServerErrorResult (500 Internal Server Error). |
![]() |
InternalServerError(Exception) |
Creates an ExceptionResult (500 Internal Server Error) with the specified exception. |
![]() |
Json<T>(T) |
Creates a JsonResult<T> (200 OK) with the specified value. |
![]() |
Json<T>(T, JsonSerializerSettings) |
Creates a JsonResult<T> (200 OK) with the specified values. |
![]() |
Json<T>(T, JsonSerializerSettings, Encoding) |
Creates a JsonResult<T> (200 OK) with the specified values. |
![]() |
MemberwiseClone() |
(Inherited from Object.) |
![]() |
NotFound() |
Creates a NotFoundResult. |
![]() |
Ok() |
Creates an OkResult (200 OK). |
![]() |
Ok<T>(T) |
Creates an OkNegotiatedContentResult<T> with the specified values. |
![]() |
Redirect(String) |
Creates a redirect result (302 Found) with the specified value. |
![]() |
Redirect(Uri) |
Creates a redirect result (302 Found) with the specified value. |
![]() |
RedirectToRoute(String, IDictionary<String, Object>) |
Creates a redirect to route result (302 Found) with the specified values. |
![]() |
RedirectToRoute(String, Object) |
Creates a redirect to route result (302 Found) with the specified values. |
![]() |
ResponseMessage(HttpResponseMessage) |
Creates a ResponseMessageResult with the specified response. |
![]() |
StatusCode(HttpStatusCode) |
Creates a T:System.Web.Http.StatusCodeResult with the specified status code. |
![]() |
ToString() |
(Inherited from Object.) |
![]() |
Unauthorized(AuthenticationHeaderValue[]) |
Creates an UnauthorizedResult (401 Unauthorized) with the specified values. |
![]() |
Unauthorized(IEnumerable<AuthenticationHeaderValue>) |
Creates an UnauthorizedResult (401 Unauthorized) with the specified values. |
![]() |
Validate<TEntity>(TEntity) |
Validates the given entity and adds the validation errors to the model state under the empty prefix, if any. |
![]() |
Validate<TEntity>(TEntity, String) |
Validates the given entity and adds the validation errors to the model state, if any. |
We have been undergoing a major upgrade to our tech stack and here is what we are implementing now… WOOT WOOT
Cloud Formation – A service that allows the entire stack to be built, provisioned, provisioned, and torn down demand.
ELB – A load balancer that works with autoscaling to create and destroy servers as needed by the traffic load.
EC2 – Cloud-based virtual machines.
EFS – A network file system that allows multiple EC2 instances to share a file system, even across availability zones.
S3 – A Simple Storage Service designed to hold static resources, such as images and documents.
RDS – Relational Database Services that cross availability zones, provide high availability, provide fail-over, and provide backup.
Previously I posted about using a Web-API to import data from another vendor’s products into our database. One of my goals for our team has been to move away from using direct calls into our database be it by either .NET ADO, Linq or SQL commands and instead expose a means for all products to get their data needs via a service. When we built a mobile application; which is a Single Page Application, I rolled out a MVC-API using Microsoft’s framework for this. Our first big pay off on adaptability and cross platform use proved out when we needed to support a RFID Device made by Motorola. This device uses a Windows CE Embedded operating system. Below I will quickly cover how I coded to connect to the API from this device.
First things first…
I had tried to store the API url’s on the device in a XML file, but watch out because xml does not like the & symbol in any value. You can use things like & or %26 inline, but Windows CE in-application-browser does not seem to like this very much… My work around was to store the values on the device’s SQL Lite instance.
Now moving on, as you know an API will have an end point you send your query to.
For example:
https://www.mysite.org/api/schedule/{0}
The above api, requires a school id so you can pull the correct schedule for that school/site.
In my example, I will show how I pulled a set of inventory tags down from the API site.
I have a basic method called GetTags…
The api is called, several helper methods help pull the data and parse it and eventually inflate a List with POCO objects called TagItem which is a simple class with Getters and Setters.
public static List<TagItem> GetTags(string ApiURL,int SiteUId)
{
string uri = String.Format(“{0}/api/Inventory/GetTags?siteUId={1}”, ApiURL, SiteUId);
return JsonConvert.DeserializeObject<List<TagItem>>(GET(uri));
}
As you can see we pass in the GENERIC API URL and pre-pend it to the API Signature.
The real meat here is the GET method which in turn uses the GetBaseRequest and other helper methods. I will post the code below since it should be fairly obvious what it is doing…
private static string GET(string pUri, int pTimeOut)
{
HttpWebRequest request = null;
string retValue = “”;
try
{
request = GetBaseRequest(pUri, “GET”, “application/json; charset=utf-8”, pTimeOut);
retValue = ReadRequestResponse(request);
}
catch (Exception)
{
throw;
}
return retValue;
}
private static HttpWebRequest GetBaseRequest(string pUri, string pMethod, string pContentType, int pTimeout)
{
System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(pUri);
request.Headers.Add(“Authorization”, Token);
request.Method = pMethod;
request.Proxy = null;
request.KeepAlive = false;
request.Timeout = pTimeout;
request.ContentType = pContentType;
request.UserAgent = “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)”;
return request;
}
private static string ReadRequestResponse(HttpWebRequest pRequest)
{
string retval;
HttpWebResponse response = (HttpWebResponse)pRequest.GetResponse();
using (Stream responseStream2 = response.GetResponseStream())
using (StreamReader reader = new StreamReader(responseStream2))
retval = reader.ReadToEnd();
response.Close();
return retval;
}
That is the meat of it, hope that helps someone else. If you have questions please let me know.