What is the Vercel equivalent to .netlify/functions?

I'm in the process of deploying this repository: https://github.com/DataStax-Examples/astra-tik-tok using Vercel instead of Netlify.

I've converted vanilla React to Next.js, but I'm unsure how to transition the code in the Home.js file to work with Vercel:

//fetch all the tik-tok posts to your feed
const fetchData = async () => {
    const results = await axios.get('/.netlify/functions/posts')
    console.log(results.data)
    setUsers(results.data)
}

Any thoughts or suggestions on how to address this?

Answer №1

In Next.js, the equivalent of traditional backend APIs are API routes, which are implemented as serverless functions on Vercel. These API routes must be placed within the /pages/api directory.

To make your existing functions work with Next.js, you will need to make some adjustments. For instance, a function named add.js would need to be converted into an API route like the example below.

// /pages/api/add.js
export default async function handler(req, res) {
    const users = await getCollection();
    try {
        const user = await users.create(id, event.body);
        res.status(200).json(user);
    } catch (e) {
        console.error(e);
        res.status(500).json({ error: JSON.stringify(e) })
    }
};

To call this API route from the client-side code, you would reference it using /api/add.

await axios.get('/api/add')

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the Ideal Location for Storing the JSON file within an Angular Project?

I am trying to access the JSON file I have created, but encountering an issue with the source code that is reading the JSON file located in the node_modules directory. Despite placing the JSON file in a shared directory (at the same level as src), I keep r ...

Step-by-step guide on creating a personalized logic and redirecting to different pages using the useEffect hook in React or Next.js

I have recently developed a quiz application with three main pages - Junior, Senior, and SuperSenior. Depending on the selection made by the user from the dropdown menu on the homepage, I need to redirect them to the appropriate page. To achieve this func ...

Issue: Revert call exception during Thirdweb NFT Drop transaction

I enjoy working with Next.js, Thirdweb, VS Code, and Sanity Studio Here is a snippet of my code: const [claimedSupply, setClaimedSupply] = useState<number>(0) const [totalSupply, setTotalSupply] = useState<BigNumber>() const nftDrop = useNFTD ...

guide on incorporating Google Maps in a Vue.js application

Can anyone help me with displaying a Google Map using Vue.js? I have provided the code below, but I keep getting an error saying "maps is undefined" even though I have installed all the necessary dependencies for Google Maps. <div id="map"></div& ...

Mapping custom colors to paths in D3 Sunburst visualizations can add a vibrant and unique touch

Currently, I am in the process of developing a D3 Sunburst Vue component and utilizing the npm package vue-d3-sunburst for this purpose. To access the documentation for the package, please visit: https://www.npmjs.com/package/vue-d3-sunburst The document ...

"Utilize Vuejs to establish a binding between two objects when necessary

With the help of moment, my calendar generates 41 days. for (let x = 0; x < 42; x++) { context.add(1, 'd'); let day = { 'date': moment(context), 'events': [] }; } ...

Paging in Ext JS does not function properly when using local data sources

I am facing an issue with enabling paging in ExtJs4 grid. The paging toolbar appears to be functioning correctly, however, the paging feature does not seem to work within the grid itself. Can anyone provide guidance on what might be missing? Ext.onReady(f ...

When adding values, they remain in their original positions

I am populating a select dropdown with options using AJAX. These options are added based on the selection made in another dropdown. The first function that appends the data: var appendto_c = function(appendto, value, curcode) { appendto.append("& ...

Implement using a variable as a key for an object in a reducer function

I am facing an issue with constructing an object. The string, named "SKU" in this scenario is being passed through action as action.name. Although I have all the necessary data in the reducer function, I need to dynamically replace the hardcoded SKU with ...

Using React and Material UI to create a table filtering system with checkboxes

I'm currently developing a filtering feature using checkboxes for a data display in a project utilizing React and Material-UI. Is there a custom solution available within Material-UI? If not, what steps should I take to achieve this? As I am rel ...

Which event occurs first for a4j:jsFunction, reRender or oncomplete?

After running a jsFunction, I want the javascript to execute once the re-rendering is completed. I assume that the "oncomplete" javascript function is triggered after the re-rendering process, but I'm not entirely certain. Any insights on this? Appre ...

Choose the initial offspring from a shared category and assign a specific class identifier

I am trying to figure out how to apply the "active" class to the first tab-pane within each tab-content section in my HTML code. Here is an example of what I'm working with: <div class="tab-content"> <div class='tab-pane'>< ...

Use Node.js with Selenium and WebdriverIO to simulate the ENTER keypress action on

I have put in a lot of effort trying to find the solution before resorting to asking this question, but unfortunately I have not been successful. All I need to know is how to send special characters (such as the enter key and backspace) with Node.js using ...

What sets @vue/cli-plugin-unit-jest apart from vue-jest?

Can you explain the distinction between these two software bundles? @angular/cli-plugin-unit-jasmine angular-jasmin If I already have one, is there a need for the other? And in what circumstances should each be utilized? ...

In the latest version of NextJS (NextJS 13), the getServerSideProps function seems to be

I've encountered an issue with my code - console.log(data) is returning as undefined. Despite reading documentation and visiting various blogs, I can't seem to pinpoint the problem. I have confirmed that the API endpoint is functioning correctly ...

Instead of modifying the selected class, jQuery generates inline styles

Utilizing the following code to dynamically create a class: $("head").append('<style type="text/css"></style>'); var newStyleElement = $("head").children(':last'); newStyleElement.html('.move{transform: translateX(1 ...

Can $location be modified without triggering its corresponding $route?

Can you update the location path in Angular.js without activating the connected route? For example, is there a way to achieve this (see pseudo code below): $location.path("/booking/1234/", {silent: true}) ...

The resolution of Angular 8 resolver remains unresolved

I tried using console.log in both the constructor and ngOnInit() of Resolver but for some reason, they are not being logged. resolve:{serverResolver:ServerResolverDynamicDataService}}, console.log("ServerResolverDynamicDataService constructor"); console ...

Remove duplicate elements from a JSON array

How can I add each item in an array for each duplicate? Transform: [ { "uuid":"EE877ABD-932F-4B4C-AB23-B324363B0B60", "planning":[ { "uuid":"6D680178-9B05-4744-A004-163D4B3E1E84", "star ...

Interacting with an API and retrieving data using JavaScript

I have hit a roadblock. This question might be simple, but I am struggling to figure it out. I am attempting to retrieve a response from an API (mapquest), but I can't seem to navigate the response to extract the necessary information. Here is my con ...