我使用express.js
const express = require('express')
const app = express()
var bodyParser = require('body-parser');
var cors = require('cors');
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'DELETE, PUT');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.post('/api/v1/login.json', function (req, res) {
var user = login(req.body.email, req.body.password)
if user !== null {
res.status(200).json({token: "test_token"});
} else {
res.status(401).json({error: 'Niepoprawny email lub hasło.'});
}
})
app.listen(3000, function () {
console.log('server listening on port 3000')
})
const users = [
{
id: 1,
email: 'test@user.com',
password: 'password',
access_token: 'test_user_access_token'
},
{
id: 2,
email: 'second@user.com',
password: 'password',
access_token: 'second_user_access_token'
}
]
function login(email, password) {
return users.find(x => x.email === email && x.password === password);
}
当我尝试运行它时,它会返回以下错误:
if user !== null {
^^^^
SyntaxError: Unexpected identifier
我该如何解决这个问题?
答案 0 :(得分:0)
if语句的正确语法是:
if (user !== null) {
...
答案 1 :(得分:0)
基本的javascript语法:
if (user !== null) {