angularjs - Chrome complaining about html tags in app.js -
i trying make simple spa angular , node. when try test if spa working in chrome, chrome gives me peculiar error. complaining html tags in demoapp.js, though not contain html code. how can be?
error: demoapp.js:1 uncaught syntaxerror: unexpected token <
test.html
<html ng-app="demoapp"> <head> <title>my angular app</title> </head> <body> <h2>demoapp demo</h2> <div> <a href="#/partial1.html">partial 1</a> <a href="#/partial2.html">partial 2</a> <div ng-view></div> </div> <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.js"></script> <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular-route.js"></script> <script src = "demoapp.js"></script> </body> </html>
demoapp.js
var demoapp = angular.module('demoapp', ['ngroute']); demoapp.config(['$routeprovider', function($routeprovider) { $routeprovider .when('/', { controller: 'simplecontroller', templateurl: 'partials/partial1.html' }).when('/partial2', { controller: 'simplecontroller', templateurl: 'partials/partial2.html' }).otherwise({redirectto: '/'}); }]); demoapp.controller('simplecontroller', function($scope){ $scope.customers = [ {name:'victor', city:'norrköping'}, {name:'mikael', city:'göteborg'}, {name:'jocke', city:'göteborg'}, {name:'skåne', city:'ystad'} ]; $scope.addcustomer = function(){ $scope.customers.push( { name: $scope.newcustomer.name, city: $scope.newcustomer.city }); } });
server.js
//lets require/import http module var http = require('http'); var fs = require('fs'); //lets define port want listen const port=8080; //we need function handles requests , send response function handlerequest(request, response){ response.writehead(200, {'content-type': 'text/html'}); fs.readfile('./test.html', null, function(error, data) { if(error) { response.writehead(404); response.write('file not found!'); } else { response.write(data); } response.end(); }); } //create server var server = http.createserver(handlerequest); //lets start our server server.listen(port, function(){ //callback triggered when server listening. hurray! console.log("server listening on: http://localhost:%s", port); });
your problem serving index.html no matter request server.
you need change server.js this:
//lets require/import http module var http = require('http'); var fs = require('fs'); //lets define port want listen const port=8080; //we need function handles requests , send response function handlerequest(request, response){ var path; if (request.url === '/') { path = './test.html'; response.writehead(200, {'content-type': 'text/html'}); } else { response.writehead(200, {'content-type': 'text/javascript'}); path = '.' + request.url; } fs.readfile(path, null, function(error, data) { if(error) { response.writehead(404); response.write('file not found!'); } else { response.write(data); } response.end(); }); } //create server var server = http.createserver(handlerequest); //lets start our server server.listen(port, function(){ //callback triggered when server listening. hurray! console.log("server listening on: http://localhost:%s", port); });
note: bad static server, returns javascript. should @ this question , create better static server, or use nginx
Comments
Post a Comment