I am getting a 404 not found on when trying to trying to hit the domain for an api I am trying to connect to. Im new to dev so Im sure there is something silly I am doing.
The app is on node.js and am I am using Vercel. I would appreciate any guidance. Thank you in advance.
Vercel.json
{
"version": 2,
"builds": [
{
"src": "server.js",
"use": "@vercel/node"
}
]
}
server.js
const express = require('express');
const fetch = require('node-fetch');
const cors = require('cors');
const app = express();
app.use(cors());
const lastFmUsername = 'xxxx';
const apiKey = 'xxxxx';
app.get('/api/song', async (req, res) => {
try {
const response = await fetch(`https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=${lastFmUsername}&api_key=${apiKey}&format=json`);
const data = await response.json();
const tracks = data.recenttracks.track;
if (tracks.length > 0) {
const nowPlaying = tracks[0];
const trackName = nowPlaying.name;
const artistName = nowPlaying.artist['#text'];
const spotifyUrl = `https://open.spotify.com/search/${encodeURIComponent(trackName + ' ' + artistName)}`;
res.send(`Currently playing: ${trackName} - ${artistName} [${spotifyUrl}]`);
} else {
res.send('No track playing');
}
} catch (error) {
res.send('Error fetching song data');
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
package.json
{
"name": "lastfmcatcherv2",
"version": "1.0.0",
"private": true,
"description": "A Node.js server to fetch and display current song information from Last.fm.",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.18.2",
"node-fetch": "^3.3.1",
"cors": "^2.8.5"
},
"engines": {
"node": "18.x"
}
}
New contributor
Happy Zulu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.