Quick Start
If you haven't learned about alova yet, it is recommended that you read introduce alova first.
And we provide a Tutorial in 5 minutes video to help you learn alova quickly.
Installation
- npm
- yarn
- pnpm
- bun
npm install alova --save
yarn add alova
pnpm add alova
bun add alova
Create an alova instance
In alova, you need to initiate a request through an alova instance. Let's create one first. When creating an alova instance, you need to specify a request adapter. Here we recommend using the alova/fetch
request adapter, which is a wrapper based on the fetch API
and is very concise.
- esModule
- commonJS
- deno
import { createAlova } from 'alova';
import adapterFetch from 'alova/fetch';
const alovaInstance = createAlova({
requestAdapter: adapterFetch(),
responded: response => response.json()
});
const { createAlova } = require('alova');
const adapterFetch = require('alova/fetch');
const alova = createAlova({
requestAdapter: adapterFetch(),
responded: response => response.json()
});
When using adapterFetch in nodejs, the nodejs version requires
v17.5
, or you can use axios request adapter.
import { createAlova } from 'npm:alova';
import adapterFetch from 'npm:alova/fetch';
const alova = createAlova({
requestAdapter: adapterFetch(),
responded: response => response.json()
});
GET request
Send a request through alovaInstance.Get
. Since the adapterFetch
request adapter is used, a Response
instance will be received. This is very simple.
const response = await alovaInstance.Get('https://alovajs.dev/user/profile');
In an asynchronous function, you can also use await alovaInstance.Get
to wait for the response.
POST request
Submit data through alovaInstance.Post
. This is also very simple.
const response = alovaInstance.Post('https://alovajs.dev/posts', {
title: 'foo',
body: 'bar',
userId: 1
});
What to do next?
In fact, this is just a simple request example. You will learn more functions in the next chapter. Let's start learning.