2016-12-08 12 views
0

私は、角2コードにREST APIを実装しようとしているが、私は削除したとき、私は角角度2:例外:位置のJSONに予期しないトークン<0

によってExpressからデータを取得して問題を抱えています角度成分はエラーがないので、おそらくそれが原因です。私はまた、これは

var express = require('express'); 
var router = express.Router(); 
var mongojs = require('mongojs'); 
var db = mongojs('mongodb://mojtaba:[email protected]:29038/mytasklist_mojtaba', ['tasks']); 

// Get All Tasks 
router.get('/tasks', function(req, res, next){ 
    db.tasks.find(function(err, tasks){ 
     if(err){ 
      res.send(err); 
     } 
     res.json(tasks); 
    }); 
}); 

module.exports = router; 

アンギュラ私server.js

 'use strict'; 
const express = require('express'); 
const app = express(); 
const jwt = require('express-jwt'); 
const cors = require('cors'); 
const bodyParser = require('body-parser'); 
const multer = require('multer'); 
const path = require('path'); 

var tasks = require('./routes/tasks'); 

var router = express.Router(); 
var mongojs = require('mongojs'); 

app.use('/tasks', tasks); 



// Set Static Folder 
app.use(express.static(path.join(__dirname, 'client'))); 


app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: true })); 
app.use(cors()); 


const authCheck = jwt({ 
    secret: new Buffer('0u33qUTwmPD-MFf56yqJ2DeHuQncgEeR790T3Ke1TX3R5R5sylVfUNlHWyqQS4Al', 'base64'), 
    audience: 'PBNaD26w0HdAinA5QFSyABjWZNrZSx9M' 
}); 




const upload = multer({ 
    dest: 'uploads/', 
    storage: multer.diskStorage({ 
    filename: (req, file, cb) => { 
     let ext = path.extname(file.originalname); 
     cb(null, `${Math.random().toString(36).substring(7)}${ext}`); 
    } 
    }) 
}); 

app.post('/upload', upload.any(), (req, res) => { 
    res.json(req.files.map(file => { 
    let ext = path.extname(file.originalname); 
    return { 
     originalName: file.originalname, 
     filename: file.filename 
    } 
    })); 
}); 

app.get('/api/deals/public', (req, res)=>{ 
    let deals = [ 
    { 
    id: 12231, 
    name: 'Playstation 4 500GB Console', 
    description: 'The Playstation 4 is the next gen console to own. With the best games and online experience.', 
    originalPrice: 399.99, 
    salePrice: 299.99 
    }, 
    { 
    id: 12234, 
    name: 'Galaxy Note 7', 
    description: 'The Note 7 has been fixed and will no longer explode. Get it an amazing price!', 
    originalPrice: 899.99, 
    salePrice: 499.99 
    }, 
    { 
    id: 12245, 
    name: 'Macbook Pro 2016', 
    description: 'The Macbook Pro is the de-facto standard for best in breed mobile computing.', 
    originalPrice: 2199.99, 
    salePrice: 1999.99 
    }, 
    { 
    id: 12267, 
    name: 'Amazon Echo', 
    description: 'Turn your home into a smart home with Amazon Echo. Just say the word and Echo will do it.', 
    originalPrice: 179.99, 
    salePrice: 129.99 
    }, 
    { 
    id: 12288, 
    name: 'Nest Outdoor Camera', 
    description: 'The Nest Outdoor camera records and keeps track of events outside your home 24/7.', 
    originalPrice: 199.99, 
    salePrice: 149.99 
    }, 
    { 
    id: 12290, 
    name: 'GoPro 4', 
    description: 'Record yourself in first person 24/7 with the GoPro 4. Show everyone how exciting your life is.', 
    originalPrice: 299.99, 
    salePrice: 199.99 
    }, 
    ]; 
    res.json(deals); 
}) 

app.get('/api/deals/private', authCheck, (req,res)=>{ 
    let deals = [ 
    { 
    id: 14423, 
    name: 'Tesla S', 
    description: 'Ride in style and say goodbye to paying for gas. The Tesla S is the car of the future.', 
    originalPrice: 90000.00, 
    salePrice: 75000.00 
    }, 
    { 
    id: 14553, 
    name: 'DJI Phantom 4', 
    description: 'The Drone revolution is here. Take to the skies with the DJI Phantom 4.', 
    originalPrice: 1299.99, 
    salePrice: 749.99 
    }, 
    { 
    id: 15900, 
    name: 'iPhone 7 - Jet Black', 
    description: 'Get the latest and greatest iPhone in the limited edition jet black.', 
    originalPrice: 899.99, 
    salePrice: 799.99 
    }, 
    { 
    id: 16000, 
    name: '70" Samsung 4K HDR TV', 
    description: 'Watch as if you were there with the latest innovations including 4K and HDR.', 
    originalPrice: 2999.99, 
    salePrice: 2499.99 
    }, 
    { 
    id: 17423, 
    name: 'Canon t8i DSLR', 
    description: 'Capture life\'s moments with the amazing Canon t8i DSLR', 
    originalPrice: 999.99, 
    salePrice: 549.99 
    }, 
    { 
    id: 17423, 
    name: 'Xbox One S', 
    description: 'Get the latest Xbox and play the best first party games including Gears of War and Forza.', 
    originalPrice: 299.99, 
    salePrice: 279.99 
    }, 
    ]; 
    res.json(deals); 
}) 


app.listen(3001); 
console.log('Listening on localhost:3001'); 

tasks.js 2

作業ですので、データは明示

によってrecivedされているサーバのルートhttp://localhost:3001/taskでデータにアクセスすることができます.component.ts:

import { Component } from '@angular/core'; 
    import {TaskService} from './task.service'; 
    import {Task} from '../../Task'; 


    @Component({ 
     selector: 'tasks-component', 
     templateUrl: 'tasks.component.html' 
    }) 

    export class TasksComponent { 
     tasks: Task[]; 
     title: string; 

     constructor(private taskService:TaskService){ 
     this.taskService.getTasks() 
      .subscribe(tasks => { 
      this.tasks = tasks; 
      }); 
     } 

     addTask(event){ 
     event.preventDefault(); 
     var newTask = { 
      title: this.title, 
      isDone: false 
     } 

     this.taskService.addTask(newTask) 
      .subscribe(task => { 
      this.tasks.push(task); 
      this.title = ''; 
      }); 
     } 
    } 
} 

task.service.ts:

import {Injectable} from '@angular/core'; 
import {Http, Headers} from '@angular/http'; 
import 'rxjs/add/operator/map'; 


@Injectable() 
export class TaskService{ 


    constructor(private http:Http){ 
    console.log('Task Service Initialized...'); 
    } 
    /* 
    getTasks(){ 
    console.log('get tasks works'); 
    return this.http.get('/tasks') 
    .map(res => res.json()); 
    } 

    */ 
    getTasks(){ 
    return this.http.get('/tasks') 
     .map(res => res.json()); 
    } 
} 

エラー enter image description here

私はクライアントにHTMLの代わりにJSONを送信しようとしたが、それは動作しません。だから何をすべきか?これは誤りであるthis.http.get('http://localhost:3001/tasks')

+2

あなたのJSONデータを共有することはできますか?私は問題がur jsonデータにあると思う。 –

答えて

0

変更return this.http.get('/api/tasks'は、あなたのAJAX呼び出し(http.get)はhtmlページにアクセスしてからJSONにそれを解析しようとしていることを意味します。

あなたの特急では、/ taskの経路がありますが、あなたのサービスでは/ api/taskにアクセスしようとしていますが、これは存在しないと思います。

express apiを変更するか、http.getメソッドを変更する必要があります。

だから、次のいずれか

app.get('/api/tasks', (req, res)=>{ 
    db.tasks.find(function(err, tasks){ 
     if(err){ 
      res.send(err); 
     } 
     res.json(tasks); 
    }); 
}); 

OR:いずれの場合も

getTasks(){ 
    console.log('get tasks works'); 
    return this.http.get('/tasks') 
     .map(res => res.json()); 
    } 

、あなたは正しいAPIをヒットしていることを確認する最良の方法は、お使いのネット​​ワーク]タブを見て、見ていますあなたのAJAXコールで叩いているURLをコピーして、あなたのブラウザ(またはより良い郵便配達員)にコピーして、何がレスポンスであるかを確認してください。

あなたの場合、 api/task、w hichは急行サーバーによって投げられます。

0

を返すために

0

あなたは急行で正しくルートを定義していません。 すべてのエンドポイントを"/ tasks"および"アップロード"と定義しました。しかし、 "/ api/tasks" にアクセスするには を試しています。 Express ルート クラス(express.Router)またはapp.route()メソッドを使用して分散型 ルートを作成する方法。

Basic Use: 
//Using app.route() 
app.route('/api/tasks') 
    .get(function (req, res) { 
    res.send('Get a random task') 
    }) 
    .post(function (req, res) { 
    res.send('Add a task') 
    }) 
    .put(function (req, res) { 
    res.send('Update the task') 
    }) 
//Using express.Router 
var express = require('express') 
var router = express.Router() 

// middleware that is specific to this router 
router.use(function timeLog (req, res, next) { 
    console.log('Time: ', Date.now()) 
    next() 
}) 
// define the home page route 
router.get('/', function (req, res) { 
    res.send('Birds home page') 
}) 
// define the about route 
router.get('/about', function (req, res) { 
    res.send('About birds') 
}) 

module.exports = router 

ここで、ドキュメントを参照して確認してください。 https://expressjs.com/en/guide/routing.html

ミドルウェアがパイプ上で動作していることを確認してください。ルートの上に配置する必要があります。 例を参照してください: https://github.com/expressjs/body-parser

var express = require('express') 
var bodyParser = require('body-parser') 

var app = express() 

// parse application/x-www-form-urlencoded 
app.use(bodyParser.urlencoded({ extended: false })) 

// parse application/json 
app.use(bodyParser.json()) 

app.use(function (req, res) { 
    res.setHeader('Content-Type', 'text/plain') 
    res.write('you posted:\n') 
    res.end(JSON.stringify(req.body, null, 2)) 
}) 
+0

はexpress.routerを使用するようにコードを更新しましたが、同じエラーが表示されます。それはjsonの代わりにhtmlを送信するので(ネットワークをチェック)、それを修正する方法はありません。 btw私のソリューションは動作しますが、express.routerを使用したい – Mojtaba

+0

OK、ネットワーク上のHTMLデータを確認してください。それはルートのような任意のエラーhtmlは、見つかりません? – xdeepakv

+0

私はあなたを得ました、あなたは間違った方法でボディパーサーを使用しています。それはパイプです。ボディパーサーと他のミドルウェアを上に使用する必要があります。 – xdeepakv

関連する問題