Retrieve data from Last.fm API by utilizing both Node.js and Angular framework

I am currently working on implementing the node-lastfmapi track.search method into my project. I have successfully retrieved the results, but I am facing challenges in integrating them into the front end using Angular. My backend is powered by mongoDB and I have defined a schema to store the selected search result that the user wishes to save. However, before saving the result, the user must view the search results. How can I effectively display these results obtained from a third-party API called Express.js?

Below is the code snippet that I have written so far:

 app.post('/api/shows', function(req, res, next) {
    var trackName = req.body.trackName;  // This retrieves the trackName entered by the user in the search bar
lfm.track.search({
    'track' : trackName
}, function (err, track) {
       if(!track.album.title)
{
              return res.send(404, { message: req.body.trackName + ' was not found.' });

}
    });

Answer №1

I have created a straightforward code snippet to search using the Lastfmapi and send the results as JSON in Express.

var LastfmAPI = require('lastfmapi');

var express = require('express');
var app = express();
var port = process.env.PORT || 8080;

var bodyParser = require('body-parser')

var lfm = new LastfmAPI({
   //your keys here
});

app.use(bodyParser.urlencoded({ extended: false }));

app.post('/api/shows', function(req, res, next) {
    var trackName = req.body.trackName;
    console.log('get request');

    lfm.track.search({ 'track' : trackName }, function (err, track) {

        res.json(track);
    });
});


console.log('Start listening! on port: ' + port);
app.listen(port);

If you want to save some results from the Lastfm API response in MongoDB, create your own Mongoose model and integrate it into my snippet.

As for why AngularJs was mentioned in your question, I'm not sure. Nevertheless, you can utilize $resource https://docs.angularjs.org/api/ngResource/service/$resource to fetch data from a RESTful API in your frontend application.

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

Having to click twice in order to close the jQuery dialog box can be frustrating

I've been attempting to set up a div that pops up using jQuery dialog. Initially, when the user clicks on the button and opens the dialog, it closes with the first click. The second time they try to close the dialog, it will reopen the same popup, r ...

The division element nested within a select tag

HTML: <div class="row"> <div class="form-group"> <div class="col-xs-10"> <label for="class_code_reservation">Class Code</label> <select type="text" class="form-control" id="class_code_reservation" na ...

Assigning a unique identifier to each table row when iterating through a collection using

I have successfully implemented code for generating a table dynamically with angularjs: <div class="col-lg-10 col-sm-10 col-xs-10"> <table class="table table-hover"> <thead> <tr> <th>item</th> <th> ...

Passing an HTML5 video element as a prop in Vue.js

I am attempting to pass an HTML5 video as a property from a parent component to a child component in Vuejs. Parent Component: <template> <div> <video ref="video"> <source src="@/assets/video.mp4" type= ...

Socketio: Issue: Isolated surrogate U+D83D is not a valid scalar value

I've been experiencing frequent crashes with my node.js server recently, all due to a recurring socket.io error. It seems that the client may be sending invalid UTF strings, causing an error in the utf8.js file. I'm getting frustrated with the co ...

Click the button to trigger the pop-up

After creating a photo gallery using a plugin, my goal is to display this gallery when the user clicks on my homepage button. I am unsure how to make this happen and would appreciate any assistance. Below is my button code: <div class="ow-button-base ...

Having trouble with installing electron on Linux and macOS?

I'm in the process of developing an application using Angular and Electron that needs to be compatible with Windows, macOS, and Linux operating systems. After successfully running the app on Windows, I encountered issues when trying to run it on macO ...

How to update an Array<Object> State in ReactJS without causing mutation

In my program, I store an array of objects containing meta information. This is the format for each object. this.state.slotData [{ availability: boolean, id: number, car: { RegistrationNumber : string, ...

Whenever the 'something' is passed in $state.go, it fails to function as intended

When using my app, I needed to check the current user so I wrote boot code for it. However, there seems to be an issue with the controller not loading when passing something in the URL. /* * boot controller */ .controller('BootCtrl', functio ...

Iterate through and conduct conditional verification

In my project using AngularJS and HTML, I have created a table to display records. I am looking for a way to iterate through the column values and strike through any value in the column that meets a certain condition. For example, in the demo provided her ...

Proper positioning of try/catch block in scenarios involving delayed async/await operations

For the past six months, I have been utilizing async/await and have truly enjoyed the convenience it provides. Typically, I adhere to the traditional usage like so: try { await doSomethingAsync() } catch (e) {} Lately, I've delved into experimenti ...

An API request was successfully completed with security measures in place; if not

I am facing a challenge with managing two tables, one for users and the other for user roles. The user role table is used to store multiple roles for each user. I have created two APIs to handle deleting a user - one API deletes the user based on their use ...

Incorporate a fresh key-value pair into the Redux Toolkit's state

I am currently working on an application that enables users to create and modify recipes. To manage the state, I have chosen to utilize React Toolkit and Redux. Here is an example of the state structure: const ingredients = [ { ingredientName: &apos ...

Tips on adjusting a JavaScript animation function

I'm currently working on adjusting the animation function within the "popup" class that controls the gallery section of a website. When the page loads, an image and background start expanding from zero scale in the center to 100 VIEWPORT HEIGHT (VH) a ...

Contrast in the way an angular controller integrates $scope

I am brand new to using Angular. I have noticed two different ways of passing the $scope object to an angular controller: app.controller("myCtrl", function($scope) { ... }); app.controller('myCtrl', ['$scope', function($scope) { ... } ...

Using TypeScript in conjunction with Node.js

I'm currently trying to use typescript with nodejs, but I keep encountering errors. Can anyone help me troubleshoot? Here is the code snippet (assuming all necessary modules are imported): import routes from "./routes/routes"; let app = express(); ap ...

What is the best way to create a placeholder for a select option with a looping value?

I have successfully implemented loops for the select options, but I needed to add a placeholder. In other words, I wanted the first value of the select options to be a placeholder, followed by the values generated from the loop. Here is the code that I u ...

What causes the error "Failed to load SWC binary for win32/x64" when using getStaticProps?

Encountering an issue while using getStaticProps() in a Next.js application, resulting in the following error when running the app: warn - Attempted to load @next/swc-win32-x64-gnu, but it was not installed warn - Attempted to load @next/swc-win32-x64-ms ...

Using React Native to create a concise text component that fits perfectly within a flexbox with a

Within a row, there are two views with flex: 1 containing text. <View style={{ flexDirection: "row", padding: 5 }}> <View style={{ flex: 1 }}> <Text>Just a reallyyyyyyyy longgggg text</Text> </View> ...

Dealing with numerous Ajax calls within a single Ajax request

I am utilizing the $http method in angularjs to retrieve multiple "parent" elements. Within the success method of these Ajax calls, I need to loop through the parent elements and make another Ajax call for each parent element to fetch its corresponding chi ...