![Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility](https://cdn.sanity.io/images/cgdhsj6q/production/97774ea8c88cc8f4bed2766c31994ebc38116948-1664x1366.png?w=400&fit=max&auto=format)
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
api-core-demo
Advanced tools
This guide will help you learn API Core by creating a simple API using Express as a provider and MongoDB as model backed by the API Core framework.
This guide will help you learn API Core by creating a simple API using Express as a provider and MongoDB as model backed by the API Core framework.
First install the dependencies and prepare the package:
$ yarn init
$ yarn add api-core api-provider-express api-modell-mongoose api-provider
$ yarn add express mongoose
$ yarn add @types/express @types/mongoose
Also create a tsconfig.json
file:
{
"compilerOptions": {
"target": "es6",
"outDir": "dist",
"module": "commonjs",
"declaration": true,
"noImplicitAny": true,
"strictNullChecks": true,
"removeComments": true,
"moduleResolution": "node",
"sourceMap": true,
"inlineSources": true,
"lib": ["es5", "es2015.promise" ]
},
"exclude": [
"node_modules", "dist"
]
}
To provide the to-be-created API, you will need a web server. This could be anything, from a server created using only the built in HTTP library of Node to almost any third-party (eg. Express, Koa, Ellipse). Anyway, you will need the matching API Core provider package.
In this demo we will use Express
and api-provider-express
.
Create the index.ts
file and add the following:
import * as express from 'express';
const app = express();
app.get('/hello', (req, res) => {
res.send('Hello World!')
});
app.listen(3333,
() => console.log('API Core DEMO - Listening on port 3333...'));
Now edit package.json
. Add the following to specify the correct entry
point and start script:
"main": "dist/index.js",
"scripts": {
"start": "node dist"
}
It's time to test your server:
$ yarn start
Now you can navigate to http://localhost:3333/hello
in a browser.
First add our API definition after the hello edge, but before the call to listen:
const path = require('path');
import {Api} from "api-core";
import {ApiProvider} from "api-provider";
const api = new ApiProvider;
api.version('2.0')
.edgeDir(path.join(__dirname, 'src/edges'))
// .relationDir(path.join(__dirname, 'src/relations'))
// .actionDir(path.join(__dirname, 'src/actions'));
Now we have an API, continue with the configuration of Express. We need to setup body parsing, handle Chrome's requests for favicon and allow access from client scripts and disable caching with the appropriate headers:
app.use(require('body-parser').json());
app.get('/favicon.ico', (req: any, res: any) => res.send(''));
app.use((req: any, res: any, next: any) => {
//Access from any domain
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,PATCH,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header('Access-Control-Expose-Headers', 'X-Total-Count');
//Disable cache
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
next()
});
app.options('*', (req, res) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.send();
});
Now we are ready to provide our API via Express:
import {ExpressApiRouter} from "api-provider-express";
api.provide(ExpressApiRouter).apply(app);
Before you could start the API, ypu have to create an edge first.
FAQs
This guide will help you learn API Core by creating a simple API using Express as a provider and MongoDB as model backed by the API Core framework.
The npm package api-core-demo receives a total of 2 weekly downloads. As such, api-core-demo popularity was classified as not popular.
We found that api-core-demo demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.